Close
Close

Nullable in C#


Reference types like array, strings, etc. are by default null if not initialized. However, value types like int, char, short, etc. can't take null value in C#.

So, if we try to assign a null value to an int (int x = null), we will get a compilation error.

But C# provides nullable type which can be used to hold null values.

We use ? operator to declare any data type as nullable type. For example, to declare int as nullable type, we need to write int?. Now, we can store null values to int. For example, int? x = null;.

using System;

class Test
{
  static void Main(string[] args)
  {
    int? x = null;
    int? y = 5;

    Console.WriteLine(x);
    Console.WriteLine(y);
  }
}
Output

5

In the above example, the value of x is null, so nothing was printed when we tried to print the value of x.

C# GetValueOrDefault


We can use GetValueOrDefault method to get the value of any nullable type and if the value is null, we will get 0. Let's give it a try.

using System;

class Test
{
  static void Main(string[] args)
  {
    double? x = null;
    double? y = 5.5;

    Console.WriteLine(x.GetValueOrDefault());
    Console.WriteLine(y.GetValueOrDefault());
  }
}
Output
0
5.5

The value of x is null, so GetValueOrDefault returned 0.

C# HasValue


We can also use HasValue to check if the nullable type has a value or null. It the value is null, then it will return false, otherwise true.

using System;

class Test
{
  static void Main(string[] args)
  {
    double? x = null;
    double? y = 5.5;

    Console.WriteLine(x.HasValue);
    Console.WriteLine(y.HasValue);
  }
}
Output
False
True

C# Null Coalescing Operator (??)


We can't implicitly convert a nullable type to a general data type. Let's take an example first.

using System;

class Test
{
  static void Main(string[] args)
  {
    int? x = 6;
    int y = x;
  }
}
Output
file.cs(9,9): error CS0266: Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)

In the above example, we got an error because we tried to implicitly convert a nullable type to a general data type (y = x), where y is general data type and x is nullable type.

However, vice-versa is true i.e., we can convert a general data type to a nullable type implicitly.

using System;

class Test
{
  static void Main(string[] args)
  {
    int? x = 6;
    int y = 5;

    x = y;
    Console.WriteLine(x);
  }
}
Output
5

We can use the ?? operator to do so i.e., to assign a value of nullable type to a non-nullable type. Let's see what ?? operator does.

result = firstOperand??secondOperand checks whether the firstOperand is null or not. If it is not null, then the value of the result will be the value of the firstOperand. Otherwise, the value of the result will be the value of the secondOperand.

using System;

class Test
{
  static void Main(string[] args)
  {
    int? x = null;
    int? y = 5;
    int? z = 6;

    int? a = x??y;
    int? b = z??x;
    int? c = y??x;

    Console.WriteLine(a.GetValueOrDefault());
    Console.WriteLine(b.GetValueOrDefault());
    Console.WriteLine(c.GetValueOrDefault());
  }
}
Output
5
6
5

int? a = x??yx is null, so the value of y was assigned to a.

int? b = z??xz is not null, so the value of z was assigned to b.

int? c = y??xy is not null, so the value of y was assigned to c.

We can also use ?? to assign nullable type to non-nullable type. Let's look at an example.

using System;

class Test
{
  static void Main(string[] args)
  {
    int? x = 5;
    int y = 6;

    int a = x??y;

    Console.WriteLine(a);
  }
}
Output
5

In this example, if the first operand is null, then the value of the second operand will be returned. In this case, the first operand is not null, so its value is assigned to a which is a non-nullable type. So, we have basically converted a nullable type to a non-nullable type.

If the first operand is null, then the value of the second operand is returned. In that case, the value will not be converted into non-nullable type if the second operand is of nullable type and we will get an error. Let's look at an example.

using System;

class Test
{
  static void Main(string[] args)
  {
    int? x = null;
    int? y = 6;

    int a = x??y;

    Console.WriteLine(a);
  }
}
Output
file.cs(10,13): error CS0266: Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)

The first operand is null, so the value of the second operand is returned but the second operand is of nullable type and a is a normal integer. So, we got an error.

In short, only the first operand is converted not the second operand.


Ask Yours
Post Yours
Doubt? Ask question