RSS 2.0 Feed
RSS 2.0


Atom 1.0 Feed
Atom 1.0

  Nullable Value Types and the New ? Syntax 


qt8gt0bxhw|20009F4EEE83|RyanMain|subtext_Content|Text|0xfbffca0000000000ca00000001000300

The C# 2.0 specification will bring a lot of changes to the language. One of those changes will be the addition of nullable value types via the “?” type. In addition to the nullable type and the “?” syntax is the “??” operator. I read about this a while back and was just reminded of it, and the fact that it is now out in the open, when I read it again on Armand du Plessis' blog.

A nullable value type is declared as follows:

//declare a nullable int variable
int? x = null;

This is telling the compiler/runtime that this int is a value type can hold the value null. You can test for null just the same way you would with a reference type.

if (x == null)
{
    //do something
}

Easy enough, but it does take some planning and fore-thought as to whether you want to allow for your value type variables to be assigned a null value (since you need to account for that in the declaration). From there you can test for null just as if it were a reference type. Or you can use the new “??” operator.

The new ?? binary operator allows you to conditionally test for null and use an alternative value instead. For example, consider the following assignment.

int myvalue = (x != null ? x : 0);

With the ?? operator, this can be rewritten as:

int myvalue = x ?? 0;

This indicates to assign the value of x to the myvalue variable or 0 in the case that x is null.

According to Ted Neward, nothing has really changed for value types with this addition:

Under the hood, nothing has changed, believe it or not; this was introduced as part of the support for generics, via the System.Nullable<X> type. Nullable<> is a generic wrapper around any value type, and introduces a boolean property "HasValue", to indicate whether the wrapper actually has a value in it, or is in fact pointing to null. The conversion between int? and Nullable<int> is purely a linguistic one, much in the same way the "conversion" between int and System.Int32 is.

There is some controversy around these new nullable types. The biggest part of the debate exists around the “lifting” of types, where a nullable value type is coerced into a value. For example, to compare a null int with a regular int and the null int will be lifted, or promoted, to an integer value - such as 0. This is why you must use the ?? operator for comparisons or other operations to avoid having the value lifted.

I'm sure they'll get it right before 2.0 comes out. As far as I know, the exact syntax and usage for nullable value types and the ?? operator is still a bit up in the air. But I think I like the it and am excited about the change. I'll have to think of some good reasons to use it and test out the waters a bit.




                   



Leave a comment below.

Comments

  1. Dave Thomas 5/19/2004 11:50 AM
    Gravatar
    how will the nullable value type affect serialization?

    If at all?

    Dave.
  2. Ryan Farley 5/19/2004 11:56 AM
    Gravatar
    Hi Dave,

    Great question. I'll test some things out and post again at what I find. Thanks.

    -Ryan
  3. sage technology 5/24/2004 12:36 AM
    Gravatar
  4. sage technology 5/24/2004 12:51 AM
    Gravatar
  5. Matt Warren 5/27/2005 10:40 PM
    Gravatar
    Your terminology is a little off, but the intention is right.

    The nullable types are not 'lifted', the operators are. The nullable values are 'propagated'.

    For example, normally if you add two int's together you get another int. Now, if one is an int? then the result type is not int, it is int?. Since the runtime does not actually have a addition primitive for int?, the normal int addition operator is used, "lifted" over the nullable types.

    basically,

    int? x = 1;
    int y = 2;

    int? z = x + y;

    this is actually encoded in the IL as

    int? z = (x.HasValue) ? x.ValueOrDefault + y : default(int?);

    The lifting operation is defined for most built-in operators on primitive value types.

    One interesting thing to note is that the lifting operation does not occur for relational operators (<,<=,>,>=). These guys are still two-value logic, not the three-value logic that languages like SQL use. If either side is null, then the result is always null.

    The equality operators are different too. Nulls do equate to Nulls, like the do today in normal C#, or C++, etc. They don't in SQL. (Well, they do sometimes, but not others.) :-)


  6. { public virtual blog; } 8/10/2006 4:52 PM
    Gravatar
    I blogged two years ago about the new nullable operator in .NET 2.0 (wow, was that really 2 whole years ago? Time flies.). Since then, I waited patiently for it's arrival. Ever since .NET 2.0 came out I've been wanting to return and blog about it again.
Comments have been closed on this topic.



 

News


Also see my CRM Developer blog

Connect:   @ryanfarley@mastodon.social

         

Sponsor

Sections