Software Development

Save time and prevent coding headaches with nullable types in C-sharp 2.0

Before C# 2.0, the inability to assign a null value to some types was a headache for developers. A new feature of C# 2.0, nullabe types, provides this much-needed ability. Zach Smith introduces C# 2.0 nullable types and explains how application developers can use them.

One headache for C# developers before C# 2.0 was the inability to assign a null value to certain types. For instance, if you declared an int, you could never set that variable to a null value. The same held true for DateTime objects. This limitation forced application developers to come up with their own solutions and workarounds for setting a non-nullable variable to null. In C# 2.0, Microsoft remedies this situation with the addition of nullable types.

Nullable types allow you to assign a null to "value types" that have previously not allowed such an assignment. Some examples of types that previously did not allow a null assignment include DateTime, int, float, and decimal.

Using Nullable Types

So how do you instantiate a nullable type? C# 2.0 provides a new "?" type modifier (a question mark) for developers to use to indicate that they want to instantiate a nullable type. The following code (Figure A) demonstrates how to instantiate several common nullable types:

Figure A

Several common nullable types

After you define the nullable type, you can set its value to null. The following code (Figure B) shows this assignment and demonstrates how to test the variable for a null value:

Figure B

Test the variable

Notice that in the parameter list of the CheckNull function shown above, we must use the ?modifier on each parameter that is nullable. If you do not use this modifier the parameter will not be a nullable type.

Nullable types also have a property called HasValue which will return true if the object has a value, and false if the object is null. Use of the HasValue property is demonstrated below in Figure C.

Figure C

HasValue

In addition to HasValue, nullable types contain a property called Value which will return the current value of the nullable object. However, if the object has been set to null, a call to the Value property will throw an InvalidOperationException with the message "Nullable object must have a value." The following code (Figure D) demonstrates the use of the Value property:

Figure D

Value property

GetValueOrDefault is also included as a property for nullable types. This property will return the current value of the object, or if the object is null, the default value for that type. For instance, if you have an "int?" object, the default value will be zero (0) if the object has no value. The following code (Figure E) demonstrates this behavior:

Figure E

GetValueOrDefault

The ??operator

C# 2.0 also comes with a new operator that can be used while working with nullable types called "??" (that's two question marks). This operator allows you to assign nullable types to non-nullable types and to define the value to use if the nullable type is null, as illustrated in Figure F.

Figure F

Operator ??

Sending nullable types to the database

It is relatively simple to send nullable types to the database and have DBNull inserted for types that have a null value. To do this we use the ??operator and cast DBNull to an object. The cast is required because the objects on either side of the ??operator must be compatible. The following code (Figure G) demonstrates how to send nullable types to the database as a stored procedure parameter:

Figure G

DBNull procedure

Save time

That explains what nullable types are and how you can use them. When used correctly, nullable types can save development time and ease program maintenance. You can use these principals in future development projects, or refactor old code to take advantage of the capabilities of nullable types.

0 comments