Nullable types are the instance of the System.Nullable struct. A Nullable type can represent the normal range of values and a null value. For example a Nullable can be assigned a value range from -2147483648 to 2147483647 or it can be assigned a null value. Another example would be a Nullable, which can be assigned the values true, false, or null. While handling databases, the ability to use null to numeric or Boolean types will a the vital role.
Characteristics of Nullable Types
- Value type variables can be assigned the value of null
- We can assign the value to a Nullable type in the same way of an ordinary value type. For example int? x=25;
- We can test the null values using the HasValue property.
- The HasValue property will return false if the variable is null and return true if the variable has a value.
- We can retrieve the value using the value property.
- The value property will return the value assigned or else will throw the exception System.InvalidOperationException
- The default value for HasValue is false and the value is undefined.
- We cannot use nested Nullable types.
- We can not use a Nullable type with a var type.
Nullable Type Declaration
We can declare a data type as a Nullable type by using the “?” operator
Int? i= null;
if you give var? i = null; , then It will throw a compile type error, but you can use var i=5;
Example 1
class Program
{
public float? salary;
static void Main(string[] args)
{
Program pTest = new Program();
Console.WriteLine("Value in Salary variable HasValue {0 }", pTest.salary.HasValue);
pTest.salary = 1000.30f;
Console.WriteLine("Value in Salary variable is {0}, [HasValue] {1}", pTest.salary.Value, pTest.salary.HasValue);
}
}
Result
The above example will return the following result as shown. 
Simple types such as int can be used with operators such as +, - and so on. There is no difference in Nullable type equivalents. Values contained in the Nullable types are implicitly converted to the required type.
Example 2
class Program
{
static void Main(string[] args)
{
int? x = 5;
int? result = x * 2;
Console.WriteLine("Answer = {0} ", result);
}
}
Here I have created two Nullable type variables namely x and result. Then the result is assigned with the outcome of x when multiplied by 2.

Suppose, if the result variable is of integer type, Then it will show compile error.
Example 3
class Program
{
static void Main(string[] args)
{
int? x = 5;
int result = x * 2;
Console.WriteLine("Answer = {0} ", result);
}
}
When you try to run the above program you will end up in the following compile time error.
Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)
The ?? Operator
The ?? Operator is called the null-coalescing operator and it is used to define the default value to the Nullable value types and reference types. We know that a Nullable type can contain value or it could be undefined. The ?? Operator will define the default value when a Nullable type is assigned to a non Nullable type.
Syntax
Op1 ?? Op2
Op1 = null ? Op2 : Op1
Example
class Program
{
static void Main(string[] args)
{
int? Op1 = null;
int result = Op1 * 2 ?? 5;
Console.WriteLine(result);
}
}
Result
When you run the above example you will get the following result.

In the above example, Since Op1 is null, Op1 * 2 will also be null. But ?? assigns the value 5 to result. We have to note that there is no explicit conversion required to put the output in the int type variable result. The ?? operator handles this conversion for you.
Also we can pass the result of a ?? evaluation into an int?.
int? result = Op1 * 2 ?? 5;
This behaviour makes the ?? operator is a versatile one to use when dealing with nullable variables.