Geeks With Blogs
Rajiv Popat blog

I first read about Nullable Types around 6 months or so ago. Since I last heard about / used Nullable types, I had completely forgotten about the syntaxes and was completely stumped when I was asked what "int? x" meant by a couple of guys at work. 

I'll not Blog about the basic concepts of Nullable Types here. I think this MSDN Article does a particularly good job at it. What I'll Blog how ever is the basic syntax which is so darn confusing at times that I've read about it once, seen it getting used twice and forgotten all about it.
 

   14         ///

   15         /// Nullable Types The Simple Format

   16         ///

   17         public static string Foo(Nullable<int> x)

   18         {

   19             if (x.HasValue)

   20                 return "You Passed A Valid Integer. Value Passed is : " + x.Value.ToString();

   21             else

   22                 return "You Passed A Null";

   23         }

So, Function Foo can now accept both nulls and Integers (but no other type). Using HasValue I can determine if null was passed to the function or not and then using Value I can actually get the Integer value if null wasn't passed.

(Needless to mention that doing a x.Value without checking is x Has a Value (x.HasValue) will result in a runtime Exception).

The same code could be written slightly differently:
 

   14        ///

   15         /// Nullable Types The Question Mark Syntax

   16         ///

   17         public static string Foo(int? x)

   18         {

   19             if (x.HasValue)

   20                 return "You Passed A Valid Integer. Value Passed is : " + x.Value.ToString();

   21             else

   22                 return "You Passed A Null";

   23         }


Notice the use of the ? character here. Saying "int? x" is the same as saying "Nullable x".

Now let's come to another scenario where I want to return 0 if the value of x is null or the actual value if x has any other valid value.
 

   14         ///

   15         /// Nullable Types The Question Mark Syntax. Return the correct Value if X

   16         /// contains a value. 0 if X contains a Null.

   17         ///

   18         public static int Foo(int? x)

   19         {

   20             int i;

   21             if (x.HasValue)

   22                 i = x.Value ;

   23             else

   24                 i = 0;

   25 

   26             return i;

   27         }

The whole if... else can be avoided using a slightly different syntax:
 

   15         ///

   16         /// Nullable Types The Question Mark Syntax. Return the correct Value if X

   17         /// contains a value. 0 if X contains a Null.

   18         ///

   19         public static int Foo(int? x)

   20         {

   21             // same as saying, if x has a value i = x; if x is null i = 0.

   22             int i = x ?? 0;

   23             return i;

   24         }

So, I could return the actual value of x if x contains a value or return any other hard-coded value of my choice if x contains null. In 99.99% (we talk about the other 0.01% of the cases later) of the cases however, I would want this "hard-coded value of my choice" to be 0 since that's the default value of integers when they are not assigned any value.

So, in the above function what I am basically saying is - if x contains a value return the value. If it doesn't, return 0 (which is the default value for value type int). I could also write this code as:
 

   15         ///

   16         /// Nullable Types The Question Mark Syntax. Return the correct Value if X

   17         /// contains a value. 0 if X contains a Null.

   18         ///

   19         public static int Foo(int? x)

   20         {

   21             return x.GetValueOrDefault();

   22         }

Since I am using Nullable types there's a 0.01% possibility that I want the Nullable type to have a different default value than what it's supposed to have. So, let's say that if X contains null I want to interpret that null as 7. I could say:

   15         ///

   16         /// Nullable Types The Question Mark Syntax. Return the correct Value if X

   17         /// contains a value. 7 if X contains a Null.

   18         ///

   19         public static int Foo(int? x)

   20         {

   21             int i;

   22             i = x ?? 7;

   23             return i;

   24         }

Or I could write it as:
 

   15         ///

   16         /// Nullable Types The Question Mark Syntax. Return the correct Value if X

   17         /// contains a value. 7 if X contains a Null.

   18         ///

   19         public static int Foo(int? x)

   20         {

   21             return x.GetValueOrDefault(7);

   22         }

So, with Nullable Types you have multiple ways of writing the same thing. This post just attempts to answer all questions I've been asked regarding Nullable Types. Final question was - how would I Convert a Nullable type to it's corresponding value type. e.g. How would I convert int? x to an integer and assign it to another integer.

As the above code snippets illustrate I would assume that the best way would be:

   13         static void Main(string[] args)

   14         {

   15             int? x = null;

   16             int y = x.GetValueOrDefault();

   17             // or int y = x.GetValueOrDefault(foo2);

   18             // where foo2 is the value I want to assign to y if x is null.

   19         }


(For all those who didn't like the black background and color of the code snippets; Sorry, but I'm now a part of a group of freaks who like to use non-white color schemes in Visual Studio :))

Posted on Saturday, September 9, 2006 6:43 AM Cookbooks And Articles , Quick Tips And Solutions | Back to top


Comments on this post: Nullable Types, What does int followed by Question-mark (int?) Mean? What's with the Double Question Marks and Other Syntaxes?

# re: Nullable Types, What does int followed by Question-mark (int?) Mean? What's with the Double Question Marks and Other Syntaxes?
Requesting Gravatar...
Hmmm this is interesting. I havent come across a situation where i would use it ..maybe in the future - for now and thus far in my short career if its null then well time to throw a exception {if there is some kind of validation check } :)
Left by Thanesh on Sep 21, 2006 9:35 AM

# re: Nullable Types, What does int followed by Question-mark (int?) Mean? What's with the Double Question Marks and Other Syntaxes?
Requesting Gravatar...
Actually, this (and the whole generics umbrella) is just a fancy "type safe" version of boxing and unboxing :)

And becuase it's type safe we're taking the concept and putting it to a use in a lot of projects to make our life's a little more interesting - for example business objects where a field is optional in DB then the correspoding business object property itself can now "literally" pass a null to the DB for optional fields, even if the property belongs to a value-type etc.
Left by rajiv on Sep 21, 2006 1:20 PM

# re: Nullable Types, What does int followed by Question-mark (int?) Mean? What's with the Double Question Marks and Other Syntaxes?
Requesting Gravatar...
"now "literally" pass a null to the DB for """"optional fields""""""" -

I guess thats the KEY WORDS RIGHT :)
Left by Thanesh on Sep 22, 2006 5:18 AM

Your comment:
 (will show your gravatar)


Copyright © Rajiv Popat | Powered by: GeeksWithBlogs.net