Tuesday, 2 September 2008

Nullable Types

When using the System.Nullable syntax you can substitute it for T?

For example,

System.Nullable<int> intNullable; is the same as
int? intNullable;

If you have declared a non-nullable variable and are assigning it a value that could be null you can use the ?? syntax to give it a default value. For example:

int? intNullable = null;
int intNotNullable = intNullable ?? 99;

Would give the intNotNullable variable a value of 99 in the event of intNullable being null.

No comments:

Free Hit Counter