August 12, 2005 tools

Very Cool Nullable Fix

.NET 2.0 has the idea of a nullable” type built right in, e.g.

Nullable<int> x = null; // legal

This adds nullability to value types as well as reference types. Further, C# adds direct support with this syntax:

int? x = null; // legal

However, while the C# language was updated to support nullability, the CLR was not, which lead to problems with boxing:

int? x = null;
object y = x;
// a boxed Nullable<T> was never null
if( y != null ) Console.WriteLine(“Doh!“);

This problem was fixed this late in the .NET 2.0 game by getting a bunch of folks together to rejigger things so that the following works the way you expect:

int? x = null;
object y = x;
// a boxed Nullable<T> can now be null
if( y == null ) Console.WriteLine(“Wahoo!“);

Somasgar has the full scoop.