Nullable types in c#

Reference types in c# can have non-existing value with a zero reference. However, For value types this is usually not possible:

string s = null; // OK, reference-type
int i = 0; // compile-time, value-type can not be null

To represent zero in a value type, you need a special construct called Nullable Type . To use a nullable type, the value type is followed by a question mark ?.
Example of nullable type integer:

int?i = 0; // ok since it is nullable type
Console.WriteLine(i == 0) // true

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.