C#: Nullable Types and ?? Operator

C#: Nullable Types and ?? Operator

 

Nullable types vs Non-nullable types

 

While developing C# projects it is common to perform null comparison operation in order to avoid null exceptions. This simple operation is mainly coded using the “var x = null” code example inside of an if clause. However not all types of variables are nullable, which means that setting a variable to null is not allowed in every case, it depends on what kind of variable you are defining. But what if there was an extension to your non-nullable type that would convert your variable types to nullable? This extension really exists.

As I said before in C# you have nullable types which represent all the values of an underlying type, and an additional null value and can be declared easily using “T?”, where T is the type of the variable and for example the normal int type cannot be null, so it is a non-nullable type, however if you define a “int?” your variable can be null, what you do is convert a non-nullable type to a nullable type.

Example:

int x=null;     Not allowed

 

image

 

int? x=null;   Allowed

 

image

 

While using nullable types you can check if a variable is null the same way you do it with non-nullable types and a constant:

 

image

 

What about Setting a Default Value when a certain variable is null?

 

In this case the C# .net framework lets you set a default value when you try to assign a nullable type to a non-nullable type, using the ?? operator.

If you don’t use this operator you can still catch the InvalidOperationException which is thrown in these cases.

For example  without the ?? operator :

 

image

 

Using the ?? operator your code becomes cleaner and more easy to read and you get a bonus, you can set a default value for multiple variables using the ?? in a chain set.

 

image

 

Nullable types vs Nullable fields


There is another interesting case of working with DataSet and DataRow when fields can be nullable. Recently I had this problem myself:

I had written this code:

start_time = Convert.ToDateTime((row["start_time"] ?? DTSqlMinDate));

And got this exception in run-time:
"Object cannot be cast from DBNull to other types".

So, there is very important distinction between null and nullable fields (columns) in the database. In the latter case they have DbNull.Value.

I wrote this code first to fix the problem:

Convert.ToDateTime((row["start_time"]== DBNull.Value)?DTSqlMinDate:row["start_time"]);

 However, this is too complicated. Rob Jasinski from UniversalThread suggested much better alternative:

start_time = row.Field<DateTime?>("start_time") ?? DTSqlMinDate;

 I am now often using this syntax instead of more complicated Convert syntax when I am working with DataRow.
Leave a Comment
  • Please add 5 and 1 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Richard Mueller edited Revision 11. Comment: Replace RGB values with color names in HTML to restore colors

  • Richard Mueller edited Revision 10. Comment: Removed blank headers in HTML to fix TOC

  • Naomi  N edited Revision 9. Comment: Minor edit

  • Naomi  N edited Revision 8. Comment: Fixed formatting at the bottom

  • Naomi  N edited Revision 7. Comment: Trying to fix bottom part formatting

  • Naomi  N edited Revision 6. Comment: Added subtitle to last case

  • Naomi  N edited Revision 5. Comment: Typo

  • Naomi  N edited Revision 4. Comment: Corrected typos and added interesting case at the bottom in regards to null vs DbNull.Value

  • Richard Mueller edited Revision 2. Comment: Removed [en-US] from title, modified title casing, added tags

Page 1 of 1 (9 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Richard Mueller edited Revision 2. Comment: Removed [en-US] from title, modified title casing, added tags

  • Richard Mueller edited Revision 3. Comment: Replace RGB values with color names in HTML to restore colors

  • Naomi  N edited Revision 4. Comment: Corrected typos and added interesting case at the bottom in regards to null vs DbNull.Value

  • Naomi  N edited Revision 5. Comment: Typo

  • Naomi  N edited Revision 6. Comment: Added subtitle to last case

  • Naomi  N edited Revision 7. Comment: Trying to fix bottom part formatting

  • Naomi  N edited Revision 8. Comment: Fixed formatting at the bottom

  • Naomi  N edited Revision 9. Comment: Minor edit

  • Richard Mueller edited Revision 10. Comment: Removed blank headers in HTML to fix TOC

  • Richard Mueller edited Revision 11. Comment: Replace RGB values with color names in HTML to restore colors

Page 1 of 1 (10 items)