.NET Tip: Using a Nullable Value Type


Application Security Testing: An Integral Part of DevOps

How often do your programs grab some data from a database and throw the values returned into local variables without giving the data another thought? I used to, until my programs started throwing exceptions because of nulls in the data. So, what did I do? I checked every field coming back from the database that could possibly be null before storing it in a local variable. Depending upon what your database allows, this could lead to an explosion of code. Here is an example of the type of code that was used to check for a null value before storing it in a local variable. In this case, the BranchCode from a DataReader is being checked:

double BranchCode;
if (dr["BranchCode"] == DBNull.Value)
   BranchCode = -1;
    BranchCode = dr["BranchCode"];

This worked, but later in the code the "magic value" of -1 would have to be checked to see whether the variable had a valid BranchCode.

if (BranchCode <> -1)

Nullable types allow a cleaner solution to this problem. You can store the data returned from the database into a nullable variable without worry about an exception being thrown at that time. You still may have to perform the check for a valid value later, depending upon exactly how you are using the variable. You can declare a value type variable as nullable using the ? type modifier. Nullable types have two read-only properties that you use to get information about the variable. HasValue returns false if the value is null; otherwise, it returns true. If HasValue returns true, you can access the Value property, which contains the currently stored value. If you attempt to use the Value property when HasValue is false, an exception will be thrown. Here is what the example above would look like using a nullable type:

double? BranchCode;
BranchCode = dr["BranchCode"];

if (BranchCode.HasValue)
   switch (BranchCode.Value)

In addition to checking HasValue, you can also just compare the value to null, like this:

if (BranchCode != null)

You do need to use some caution, however, when using nullable types. Nullable numeric types can be used in comparisons and expressions just like normal numeric expressions. If the nullable variable has a non-null value, everything will work as you would expect. If the nullable variable is null, you need to realize that the result of the expression could be null and make sure that case is handled appropriately.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.


  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.