Nullable Types in .Net C#

Since .NET version 2.0, the new feature introduce which is nullable types.  In the basic programming, how do you assign null to the promitive type such as int, long, and bool. Its is impossible.

The following code shows how to define an integer as nullable type and checks if the value of the integer is null or not.


/// <summary>
/// Nullable types
/// </summary>
public static void TestNullableTypes()
// Syntax to define nullable types
int? counter;
counter = 100;

if (counter != null)

// Assign null to an integer type
counter = null;
Console.WriteLine("Counter assigned null.");

Console.WriteLine("Counter cannot be assigned null.");

If you remove the "?" from int? counter, you will see a warning as following:

NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

Phone book application example - Java

Modal Popup Message Box ASP.NEt C# Example