Nullable Type in C#

Introduction:

Nullable types are introduced in C#2.0. Primitive data type like int, long cannot be assign null.

int x = null;//Compile time error

Example:

Nullable<int> i = null;

Nullable Types are instance of System.Nullable<T> struct.

[Serializable]

public struct Nullable<T> where T : struct

{

public bool HasValue { get; }

public T Value { get; }

}

HasValue and Value Property:

Nullable types have a property called HasValue. It tells that int data type has value or not. It returns true when there is value otherwise returns false. Value property returns value of variable.

Example:

public class Program

{

static void Main(string[] args)

{

Nullable<int> x = null;

if (x.HasValue)

{

Console.WriteLine(x.Value);

}

else

{

Console.WriteLine(“null”);

}

Console.ReadLine();

}

}

Value property will throw exception if value assigned is null and we are accessing the value.

clip_image002

How to access value of Nullable type:

GetValueOrDefault() used to get value of nullable types. It will not throw any exception if value is null. It will return default value 0 if value is null.

static void Main(string[] args)

{

int? i = null;

Console.WriteLine(i.GetValueOrDefault());

Console.ReadLine();

}

Shorthand Syntax for Nullable Types:

We can use the ? operator instead of using Nullable<T>.

Ex: int? ,Long?.

int? x = null;

long? y = null;

Type Casting:

Explicit type casting is required if type cast from nullable types to non-nullable types otherwise it will throw error.

static void Main(string[] args)

{

int? i = 10;

int x = (int)i;

Console.WriteLine(x);

Console.ReadLine();

}

When to Use:

Most of application uses database. Database have different types. There is no concept of value type and reference type. All data types like int, DateTime, char can hold null in database.

But how to store those null value in C# because primitive data types cannot assign null value. Nullable types allow to assign null value.

Summary:

  • Nullable types can only be used with value types.
  • ? operator is shorthand syntax for nullable types.
  • Value property is used to get value of nullable types.
  • The value property will throw InvalidOperationException if value is null else it will return value.
  • The HasValue property returns true if variable contains a value or false if value is null.
  • Nested nullable types are not allowed. Nullable<Nullable<int>> i. It will give compile time error.
  • We can use == and != operators with nullable type.
  • We can also use null-coalescing operator(??) to assign value of nullable type.
  • GetValueOrDefault() method is used to get value of nullable Type. If returns 0 if value is null.
  • Nullable types are used in database programming.
  • Nullable types do not support var type. Compiler will give compiler error if used with var.
  • Explicit type casting is required to type cast from nullable types to other data type

Be the first to comment

Leave a Reply

Your email address will not be published.


*