Pages

Friday, 12 April 2013

C# Nullable Types: Why and why not to use Nullable Types in C#?

C# Nullable Types: Why and why not to use Nullable Types in C#?

Nullable types in the C# language can be constructed by specifying the question mark after a value type in a declarator statement. The nullable int can be specified with the syntax "int?"

Nullable types are described in the C# language specification as being value types that are wrapped inside the nullable type, and they can then be unwrapped dynamically and implicitly.

Following program demonstrates a use of the nullable integer type, which can be specified with the int? syntax. You can use a question mark at the end of a value type to transform the type into a nullable type. The program demonstrates a null int type, the HasValue and Value properties, and using the nullable type to acquire the value of the instance.

Program that uses nullable int type C#

using System;
class Program
{
    static void Main()
    {
       // Assign null to an integer            
       int? value = null;
       Console.WriteLine(value.HasValue);
       // Assign the nullable integer to a constant integer. The HasValue property is now true.
       value = 1;
       Console.WriteLine(value.HasValue);
       Console.WriteLine(value.Value);
       Console.WriteLine(value);
       if (value == 1)
       {
         Console.WriteLine("True");
       }
    }
}

Output
False
True
1
1
True

Nullable<int> is the same as int?. When you use a type such as int?, you have access to extra properties such as HasValue and Value.

The HasValue instance property on the nullable type returns a Boolean that indicates whether the nullable instance contains a set value. If the type is null, it does not have a value and HasValue is false. If the type is assigned to an integer, the HasValue property is true. Also, if the HasValue property is true, you can access the Value property without an exception.

Let's examine the implementation of the nullable generic struct in the base class library.

When you use a type such as int?, the C# compiler actually uses the Nullable<T> struct, where T is the value type you are using such as int. Structs in the C# language are allocated in continuous memory for performance reasons, and this makes nullable types fairly efficient.

Why to use Nullable Types in C#?

1. Nullable<T> is useful for when you need a possible invalid state for a value type.

2. If the data is being retrieved from a database that may contain a null value for the column.
 
Why not to use Nullabe Types in C#?

1. There is overhead to using nullable types and this translates to reduced performance over raw value types in some cases.

2. If nullable types are used incorrectly, these will unnecessarily increase the complexity of your code.

3. You also have to be careful to avoid null dereferences, so they place additonal burden on programmers working with that code.

No comments:

Post a Comment

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.