Nullable Types in C#

Peermohamedmydeen
Posted by Peermohamedmydeen under C# category on | Points: 40 | Views : 1910
Hi,
In C# we cannot assign a null value to an integer directly.

In otherway we can assign a null value to an integer using '?
'
We can check the null-ability of an integer by using nVal.HasValue Property.
The below code snippet will help you to understand the nullable types better

int? nVal=null;
nVal=5;
if(nVal.HasValue)
{
Console.WriteLine("Value:{0}",nVal.ToString());
}
else
{
nVal=nVal ?? 10;
Console.WriteLine("Value:{0}",nVal.ToString());
}
Here the ' ?? ' operator used to Check whether nVal is null or not.If its null it will return 10.

Thanks,
PMM :)

Comments or Responses

Login to post response