Nullable Types In C#

Why should we use Nullable Type?

This is my Heading

Blockquotes are very handy in email to emulate reply text.

 

There are a number of scenarios where it will be good to use a nullable type. Look at the below scenarios,

  1. Nullable Type is a special type of value-type that can contain a value of their normal range as well as null value. A value type variable cannot contain null value so we can use nullable type which has the ability to contain a value of its normal range as well as null.

     
    
  2. We cannot assign null to int, bool, double, char values while retrieving values from the database. So it is recommended to use nullable types instead of value types in a case where we want to store the null value.

     
    
  3. Useful with binding in WPF to store a default value when UI property bind does not contain any data.

     
    
  4. In many scenarios, we use reference type only to store a null value. In these cases, reference type can be replaced with nullable type. 

Likewise, there are numerous scenarios where nullable is useful.

 

What is Nullable Type?

 

A Special type of value type variable which can contain the value of its normal range as well as null value.

 

Syntax of Nullable Types is,

  1. <data_type>? <variable_name> = value;  
  2. int? a = 25;  
  3. //or  
  4. Nullable<<data_type>> <variable_name> = value;  
  5. Nullable<int> a = 25; 

We can use any one of them, both are the same

 

Nullable of bool type can contain true and false as well as null. Nullable of int type can contain any value from the range of int(-2147483648 to 2147483647) as well as null.

  1. bool? a= true;  
  2. int? b= null;  

Nullable Types can only be used with value-type, not with a reference type as they already contain the null value.

  1. Nullable<string> str = "75";  

This will generate a compile time error as "The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable<T>' Name can be simplified". The reason is 'string' is a reference type and it already contains the null value. Nullable can only be used with value-types.

 

Instances of the System.Nullable<T> struct.

 

Nullable types cannot be nested.

  1. Nullable<Nullable<bool>> value;  

This will generate a compile time error as "The type 'bool?' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable<T>' Name can be simplified". The reason is 'string' is a reference type and it already contains the null value. Nullable can only be used with value-types.

  1.  

In the program shown above, we created two nullable variables of int type 'a' and 'b' and a method named 'IsValue' whose return type and arguments are of nullable types. The method will take two parameters of Nullable<int>. The 'HasValue' property returns true if the variable associated with it contains a value, or false if it is null. If both the numbers(num1 and num2) contains value then method will return true else false. If there will occur an exception then null will be returned from the catch block.

 

Conclusion

 

Nullable Types used to include null as a value for value type variable. It has a lot of advantages and is most preferred when dealing with the database. One of the cons is the complexity and overhead as we have to do null checks more frequently.

using System;  

namespace Tutpoint  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            int? a = 300;  
            int? b = 55;  
            Program p = new Program();  

            p.IsValue(a, b);  
            Console.ReadKey();  

        }  
        public Nullable<bool> IsValue(int? num1, int? num2)  
        {  
            try  
            {  
                if (num1.HasValue && num2.HasValue)  
                {  
                    Console.WriteLine("Value of num1= " + num1 + " and num2= " + num2);  
                    return true;  
                }  
                else  
                    return false;  

            }  
            catch (Exception)  
            {  
                return null;  
            }  
        }  
    }  

}

Thank you. Please feel free to ask any question or make a suggestion.

Ebook Download
View all
Learn
View all