Csharp Nullables
Csharp Nullables
C# provides a special data types, the nullable types, to which you can assign normal range of
values as well as null values.
For example, you can store any value from -2,147,483,648 to 2,147,483,647 or null in a
Nullable<Int32> variable. Similarly, you can assign true, false, or null in a Nullable<bool>
variable. Syntax for declaring a nullable type is as follows:
using System;
namespace CalculatorApplication
{
class NullablesAtShow
{
static void Main(string[] args)
{
int? num1 = null;
int? num2 = 45;
double? num3 = new double?();
double? num4 = 3.14157;
When the above code is compiled and executed, it produces the following result:
If the value of the first operand is null, then the operator returns the value of the second operand,
otherwise it returns the value of the first operand. The following example explains this:
using System;
namespace CalculatorApplication
{
class NullablesAtShow
{
static void Main(string[] args)
{
double? num1 = null;
double? num2 = 3.14157;
double num3;
num3 = num1 ?? 5.34;
Console.WriteLine(" Value of num3: {0}", num3);
num3 = num2 ?? 5.34;
Console.WriteLine(" Value of num3: {0}", num3);
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following result: