blog

Home / DeveloperSection / Blogs / Nullable Types in C#

Nullable Types in C#

priyanka kushwaha2301 03-Mar-2015

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

Updated 22-Feb-2018

Leave Comment

Comments

Liked By