Friday, October 08, 2004

Much ado about Null

There is a proposal to include Nullability in C#. This is the ability to set NULL values for all types including value types (primitive types). This can be very useful when dealing with databases. Ofcourse there are other ways to handle NULL and value types without direct language support, but none of them are without shortcomings. For eg, we can use -1 for unsigned integers, but such an approach is useful only when such an unused value is available (and identified). Another way is to use a boolean indicators in separate variables, but this does not work well for parameters and return values. We can even use a set user-defined nullable types, but, then again, this works for only a closed set of types.
C# plans to have nullable types that doesnt have any of theses shortcomings by providing complete and integrated support for nullable forms for all value types. We will use the type modifier ? to indicate a nullable type. For eg, int? is used to indicate a nullable int. Note that the underlying type will be int itself. We can implement a nullable type using a structure that contains the underlying type as well as a null indicator. There will be two read-only properties -
- HasValue of type bool and and
- Value of the nullable type's underlying type.
The operation is simple. For non-null instances, the HasValue will have value true, and false otherwise. If HasValue is true, Value will return value of the instance, else an exception is thrown.

For those with a musical inclination can check my share for the new additions :
Bryan Adams - Room Service
Essential R&B - Love Collection (2 CD)
Leonard Cohen - Dear Heather

----
Reference:
MSDN - Nullable Types specification.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home