In C#, the nullable type allows us to assign null
to the variable. For example,
Nullable<int> x = null;
Here, we have assigned null
value to the integer type variable x
.
Declare Nullable Type
In C#, we can declare the nullable type as:
Nullable<dataType> variableName = null;
Here, dataType
is Value Data Types like floating point type, integer type, boolean type etc.
Another way to declare Nullable Type
We can also declare Nullable
Type using ?
operator as:
datatype? variableName = null;
For example,
int? x = null;
Why Nullable Type?
In C#, we cannot directly assign null value to a variable type. For example,
using System;
class Program
{
public static void Main()
{
// assigning 'null' to x
int x = null;
Console.WriteLine(x);
}
}
This code throws a compiler error:
Cannot convert null to 'int' because it is a non-nullable value type
So in place of that, we use the Nullable
type to assign null
to the variable as:
using System;
class Program
{
public static void Main()
{
// assigning 'null' to x
Nullable<int> x = null;
Console.WriteLine(x);
}
}
The above program does not throw an error.
Access the Nullable Type
To access the value of the Nullable
Type, we use the GetValueOrDefault()
method. For example,
using System;
class Program
{
public static void Main()
{
// assigning 'null' to integer type variable x
Nullable<int> x = null;
// access Nullable type
Console.WriteLine("Value of x: " + x.GetValueOrDefault());
}
}
Output
Value of x: 0
In the above example, we have assigned null
to the integer type variable x
.
Nullable with Different Data Types
In C#, the Nullable
types work only with the value data types like integer types, floating point types, boolean types, etc. For example,
using System;
class Program
{
public static void Main()
{
// assigning 'null' to integer type variable x
Nullable<int> x = null;
// assigning 'null' to boolean type variable y
Nullable<bool> y = null;
// assigning 'null' to floating point type variable z
Nullable<float> z = null;
// access Nullable types
Console.WriteLine("Value of x: " + x.GetValueOrDefault());
Console.WriteLine("Value of y: " + y.GetValueOrDefault());
Console.WriteLine("Value of z: " + z.GetValueOrDefault());
}
}
Output
Value of x: 0 Value of y: False Value of z: 0
Here, we have assigned null
to different types of variables.
Frequently Asked Questions
We have to assign a value to the nullable type while declaring the variable. Otherwise, the program gives a compile-time error. For example,
using System;
class Program
{
public static void Main()
{
// a is not assigned with any value
Nullable<int> a;
Console.WriteLine(a);
}
}
Error:
Use of unassigned local variable 'a'
We can use Nullable.HasValue
or Nullable.Value
to check if a variable is assigned with a value or not. It returns:
True
- if a variable is assigned with a valueFalse
- if a variable is assigned withnull
And, gives a compile-time error if the variable is not assigned with a value. For example,
using System;
class Program
{
public static void Main()
{
// x is assigned with a value
Nullable<int> x = 5;
// y is assigned with null value
Nullable<int> y = null;
// returns True since x is assigned with a value
Console.WriteLine(x.HasValue);
// returns False since y is assigned with a null value
Console.WriteLine(y.HasValue);
}
}
Output
True False