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.
If you remove the "?" from int? counter, you will see a warning as following:
By Mohd Zulkamal
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 =)
The following code shows how to define an integer as nullable type and checks if the value of the integer is null or not.
Example
/// <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.");
}
else
{
Console.WriteLine("Counter cannot be assigned null.");
Console.ReadLine();
}
}
If you remove the "?" from int? counter, you will see a warning as following:
By Mohd Zulkamal
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 =)