Run this to see what int?is the type of value:
class Program
{
static int? nullInt;
static void Main(string[] args)
{
nullInt = 2;
Console.WriteLine(string.Format("{0} + 3 != {1}", nullInt, DoMath(nullInt , 3).ToString()));
Console.WriteLine(string.Format("{0} * 3 = {1}" , nullInt , DoMultiply(nullInt , 3).ToString()));
nullInt = null;
Console.WriteLine(string.Format("{0} + 3 != {1}" , nullInt , DoMath(nullInt , 3).ToString()));
Console.WriteLine(string.Format("{0} * 3 = {1}" , nullInt , DoMultiply(nullInt , 3).ToString()));
Console.ReadLine();
}
static int? DoMath(int? x , int y)
{
if (x.HasValue)
{
return (++x) + y;
}
else
return y;
}
static int DoMultiply(int? x , int y)
{
if (x.HasValue)
{
return (int)x * y;
}
else
return 0;
}
}
I found that they are very interesting and make some clever uses.
, ? , , . , - HasValue ( )? Nullable< T > , Value - .