Nullable Types in .Net 2.0

In .Net 2.0, While working with Value types ( All numeric data types – Boolean, Char, and Date All structures, even if their members are reference types Enumerations, since their underlying type is always Byte, Short, Integer, or Long )
we usually face one common problem that these value types doesnt support Null Value. ( in past I met with this problem while passing Null values to Datetime object to databse,also some integer values which are defined NUllable in databse , but I am not able to pass blank string (“”) or 0 in tht cases through .Net code )

eg int abc = null; compiler throws an error
“Cannot convert null to ‘int’ because it is a value type”

So With the help of Nullable types we can solve this problem, Nullable types represent value-type variables that can be assigned the value of null,it is a new feature in both C# and

We can’t create a nullable type based on a reference type.
(Reference types already support the null value. eg string x = null )

For example, a Nullable, pronounced “Nullable of Int32,” can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value. A Nullable can be assigned the values true or false, or null.

The ability to assign null to numeric and Boolean types is particularly useful when dealing with databases and other data types containing elements that may not be assigned a value. For example, a Boolean field in a database can store the values true or false, or it may be undefined.

syntax for declaring a nullable type. Rather than typing:
Nullable x = new Nullable(41); instead in simple we can also use as
int? x = 41; and this is the way it is used generally.
The syntax T? is shorthand for System.Nullable, where T is a value type. The two forms are interchangeable.

Nullable types are instances of the System.Nullable struct. A nullable type can represent the normal range of values for its underlying value type, plus an additional null value.
The two fundamental members of the Nullable structure are the HasValue and Value properties.Use the HasValue and Value read-only properties to test for null and retrieve the value, for above eg x.HasValue is True , as x is having value 41

  • The HasValue property returns true if the variable contains a value, or false if it is null.
  • The Value property returns a value if one is assigned, otherwise a System.InvalidOperationException is thrown.
  • The default value for a nullable type variable sets HasValue to false. The Value is undefined.
  • ?? operator is used to assign a default value that will be applied when a nullable type whose current value is null is assigned to a non-nullable type, for example int? x = null;int y = x ?? -1;
  • Nested nullable types are not allowed. The following line will not compile: Nullable>n;

According to Paul Wilson “Nullable Types are NOT Integrated in .NET v2.0” he mentioned the points as below, Can be help full to understand for details view above url.
Nullable value types have been added to .NET v2.0, but they have NOT really been integrated. The C# team has done a good job incorporating support for nullable types into the syntax, but the VB team has done nothing, which once again leaves VB behind the curve in features. But that is not what this post is about — this post is about the fact that nullable types have NOT been integrated into the rest of the .NET framework — and obvious places at that.

  • Reading Data: Given a data reader, you can’t just assign a value to a nullable type! This one just seems incomprehensible — nullables obviously already exist in the database, and the .NET SqlTypes support them already, so this seems to be a key integration point. But alas, you simply can NOT assign a value from your data reader to a nullable type. Instead, you still have to explicitly test for DBNull, and handle that case separately:

int? nullInt;
if (dataReader.IsDBNull(index))
nullInt = null;
nullInt = dataReader.GetInt32(index);

  • ConvertType: C# does at least support casts to/from regular types and nullable types, and it even gives you a handy way to convert nulls to a default by using the ?? operator, but just try to do any generic type conversions with Convert.ChangeType — not supported! Again this seems like an obvious integration point, if nullable types are to be “builtin”, but once again the new nullable types just feel like an afterthought — and NOT integrated.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s