Nullable Types in .NET Framework

posted on Sunday, September 13, 2009 6:00 PM

Within the .NET Framework data types such as Boolean can either be true or false. Right?  Well, with the release of 2.0 Framework it has been possible to create nullable data types.  A nullable type can represent all the values of the underlying type plus empty (or undefined).  In the case of our Boolean it would be True/False and Nothing.

 

It is only possible to create a nullable types Nullable(Of T) for value types since the default value for reference types is already nothing.  The following types are valid as nullable: Boolean, Byte, Int16, Int32, Int64, Single, Double, Decimal, DateTime.  The following examples show examples on how to create a nullable variable type:

 

Dim success as New Nullable(Of Boolean)()

Dim customerId as New Nullable(Of Integer)()

 

With the release of .NET 3.5 it is also possible to use the short form versions:

 

Dim success as Boolean?

Dim customerId as Integer?

 

In C#

 

bool? success;
int? customerId;

 

To check of a nullable type as a value it is possible to use the HasValue  or Value properties.  Use the System.Nullable.GetValueOrDefault property return either the assigned value, or the default value for the underlying type.  

 

If customerId.HasValue Then

  ‘do something

End If

 

If assigning a nullable type to a non-nullable type you must cast operator is necessary.  For example:

 

int newCustomerId = (int)customerId;  // in C#

 

Nullable types can be useful when dealing with NULL values from a database query however they are not the same.  That is to say that DBNULL value is not the same as Nothing.  When returning data from the database the following code will be necessary:

 

int? customerId = null;

if (!DBNULL.Value.Equals(reader[“CustomerId”])){    // this code converts the DBNULL to a c# null
  customerId = (int)reader[“CustomerId”];
}

category: .NET Language Features

Comments


# re: Nullable Types in .NET Framework
Posted by casinos de jeux VIP on 6/9/2010 3:09 AM
Gravatar The Nullable types can be useful when dealing with NULL values from a database query however they are not the same. That is to say that DBNULL value is not the same as Nothing. When returning data from the database the following code will be necessary..This article gives the light in which we can observe the reality. this is very nice one and gives in
depth information.Valuable information for all.I will recommend my friends to read this for sure... Thanks.....
# re: Nullable Types in .NET Framework
Posted by Kaiden on 7/1/2010 11:45 AM
Gravatar Thanks and regards.
Post Comment
Title *
Name *
Email
Url
Comment *
Please add 8 and 8 and type the answer here:

About Me

An engineer by training and a software developer at heart. My techniques and approaches meld engineering approaches with software technology.

Core to these principles is a systematic approach to the development of software with a strong lifecycle and process management emphasis through adoption of mature technologies.

Ten years designing heavy structural steel and concrete structures and 12 years in the software development profession have embedded strong project management and business knowledge in my approaches.

Subscribe to Rss Feed


Follow me on twitter @dyardy