Skip to main content
added detail about the Nullable< T > struct
Source Link
IAbstract
  • 19.9k
  • 17
  • 104
  • 153

Run this to see that the int? is a value type:

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 have found these to be very interesting and makes for some clever uses.

What ? does is create a nullable reference to an otherwise non-nullable value type. It is like having a pointer that can be checked - HasValue (a boolean value)? Nice thing about the Nullable< T > is that the Value property does not need to be cast to it's original type - that work is done for you inside the nullable struct.

Run this to see that the int? is a value type:

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 have found these to be very interesting and makes for some clever uses.

Run this to see that the int? is a value type:

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 have found these to be very interesting and makes for some clever uses.

What ? does is create a nullable reference to an otherwise non-nullable value type. It is like having a pointer that can be checked - HasValue (a boolean value)? Nice thing about the Nullable< T > is that the Value property does not need to be cast to it's original type - that work is done for you inside the nullable struct.

Source Link
IAbstract
  • 19.9k
  • 17
  • 104
  • 153

Run this to see that the int? is a value type:

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 have found these to be very interesting and makes for some clever uses.