Home > DeveloperSection > Blogs > Nullable Types in C#

Nullable Types in C#


.NET C# 
Ratings:
0 Comment(s)
 698  View(s)
Rate this:

Nullable Types in C#

In this blog, I’m explaining about nullable types  in  C#

 

Defining a nullable type  is very similar to defining the equivalent non-nullable type. The difference is in the use of the? type modifier.

int?  is a nullable value type.

? : lift operator

Example:

int? x =null;

     

            int y = 3;

            int? result = x ?? 0;

          //This operator assigned the value of 'x' to result if "x" is null else it will assign the value of "y" to "result"

           //So if first  variable is null then will be assigned else the value of second variable will  be assigned.

            Console.WriteLine(result.ToString());

Output:

0

Example:    

 int? x =null;

      int y = 3;

            int? result = x ?? y;

            Console.WriteLine(result.ToString());

Output:

3

Example:

            int? x =4;

     

            int y = 3;

            int? result = x ?? y;

            Console.WriteLine(result.ToString());

Output:

4


Don't want to miss updates? Please click the below button!

Follow MindStick