RSS 2.0 Feed
RSS 2.0


Atom 1.0 Feed
Atom 1.0

  Null-Coalescing Operator in .NET 2.0 


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

I blogged two years ago (See Nullable Value Types and the New ? Syntax, and More on Nullable Value Types) 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.

Here's a recap on the Null-Coalescing operator. It is a cleaner replacement for ternary conditional operator ?: and has some additional uses as well. Think of the SQL Coalesce function (or IsNull). It is a lot like that. It gives you the first non-null between the two as SQL Coalesce does, but it doesn't allow multiple conditions to be checked as SQL Coalesce does (unless you chain them together). If the left-side is null, it gives you the right side. The right side could be considered your “default“ in case the value on the left is null for some cases. I've always been a fan of using ?: but there was something about needing to, at times, repeat variables in the statement. Here's a sample of what I am talking about:

string name = getName();
Console.WriteLine(name == null ? "No name" : name);

For as much as I love using ?: I've just never liked having to specify the variable name twice in that line. Not a huge deal, but things are even better using the null-coalescing operator in this scenario:

string name = getName();
Console.WriteLine(name  ?? "No name");

Much nicer. Gives you the same warm-fuzzy feelings you get from the SQL Coalesce function, doesn't it? It get's even better as you think of the possibilities you have using the new ?? operator. What previously would have been this (with the ?: operator)

Customer customer = Broker.GetCustomer(id);
if (customer == null) customer = new Customer();

Now becomes this:

Customer customer = Broker.GetCustomer(id) ?? new Customer();

I feel the love. But don't forget about nullable types. Nullable types in .NET 2.0 allow you to use a use a value type that can essentially be null. For example, if you have an int, bool, or other value type, it is either uninitialized or a value. Never null. But what if you wanted/needed to indicate null for a value type? A nullable value type is declared with a ? following the type. Let's take a look at a nullable int.

int GetInt()
{
    int? id = null;
    return id ?? -1;
}

Ever since .NET 2.0 came out I've wondered why I don't see more of this syntax around. From everything I come accoss I don't find this very often. Maybe people are still getting used to it, but it sure is some cool stuff IMO.




                   



Leave a comment below.

Comments

  1. Jon Skeet 8/17/2006 9:41 PM
    Gravatar
    I discovered a really cool (IMO) use for the NCO a few weeks ago.

    What we're still missing is Groovy's safe dereferencing operator, which lets you evaluate a long expression but stops when it first hits null:

    string x = item.Buyer.FirstName;

    would evaluate to null if item or item.Buyer or the whole expression evaluates to null, but without going bang...

    Jon
  2. Jon Skeet 8/17/2006 9:42 PM
    Gravatar
    (I should have said - click on my name to go to my blog entry on the other use for the NCO.)

    Jon
  3. { public virtual blog; } 8/22/2006 4:21 PM
    Gravatar
    I last posted about the null coalescing operator in .NET 2.0 and just had to post a follow up. I came accross a post on Born 2 Code .NET (via Dennis van der Stelt) where several examples of ?? syntactic sugar are listed to demonstrate how the null coalescing operator surpasses the ternary conditional operator (?:) and if constructs as far as usefulness and readability.
  4. { public virtual blog; } 8/22/2006 4:53 PM
    Gravatar
    I last posted about the null coalescing operator in .NET 2.0 and just had to post a follow up. I came accross a post on Born 2 Code .NET (via Dennis van der Stelt) where several examples of ?? syntactic sugar are listed to demonstrate how the null coalescing operator surpasses the ternary conditional operator (?:) and if constructs as far as usefulness and readability.
  5. Logo Designers 6/16/2008 4:27 AM
    Gravatar
    Looking back, I don't think net 2.0 took off sa fast as one might expect, in spite of the vast improvements over 1.0, simply b/c even coders are slow to break out of their comfort zones with 1.0 It's not like the previous version stopped working...
Comments have been closed on this topic.



 

News


Also see my CRM Developer blog

Connect:   @ryanfarley@mastodon.social

         

Sponsor

Sections