What is Nullable types in c#?

 Posted by Sathya4260 on 2/3/2011 | Category: C# Interview questions | Views: 8312 | Points: 40

In C# as we know String is reference type and int is value type. A reference type can be assigned with a null value like string s = null; But you can not assign a null value directly to a integer. like int a = null So to make the value type accept a null value, nullable types are used. To make it nullable, a ? is added . in ? a = null;

Value that gets or sets the value. A DateTime? will have a Value of type DateTime, an int? will have one of type int, etc.
HasValue that returns true if it's not null.

You can always convert a value type to a nullable type:

Ex: DateTime myDate = DateTime.Now;
DateTime? myNullableDate = myDate;

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response