Author : MD TAREQ HASSAN | Updated : 2021/03/22

What is Nullable Type?

Nullable Value Type

// Nullable types are VALUE types that can take null as value. The default value of a nullable type is null.
int? x = null;     // no value

char? letter = 'a';

bool? flag = null;

// An array of a nullable value type:
int?[] arr = new int?[10];

Nullable Value Type to Normal Value Type

int? x = 10;
int foo = x ?? -1;
Console.WriteLine($"foo is {foo}");  // output: foo is 10

x = null;
int bar = x ?? -1;
Console.WriteLine($"bar is {bar}");  // output: bar is -1

Nullable Reference Type

HasValue and Value

For “string? x”, you might need to use “string.IsNullOrEmpty(x)