RSS 2.0 Feed
RSS 2.0


Atom 1.0 Feed
Atom 1.0

  More Null-Coalescing (??) Operator Love 


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.

Here's a great sample from the post:

public Brush BackgroundBrush
{
    get
    {
        return _backgroundBrush ??
        (
            _backgroundBrush = GetBackgroundBrushDefault()
        );
    }
}

Which translates to (using a traditional if construct)

public Brush BackgroundBrush
{
    get
    {
        if (_backgroundBrush == null)
        {
            _backgroundBrush = GetBackgroundBrushDefault();
        }
        return _backgroundBrush;
    }
}

The idea here is that the ?? is evaluated first before the return, causing the assignment to occur in the case when the _backgroundBrush variable is null. That is just plain cool. Make sure you check out the link to the post on Born 2 Code .NET for more examples. Also check out Jon Skeet's post on doing elegant comparisons using the null coalescing operator as well.




                   



Leave a comment below.

Comments

  1. SimoneB 8/23/2006 10:04 AM
    Gravatar
    Thanks Ryan, interesting. I didn't realize that using parenthesis could let me do assignments. Have you noticed that the more you exploit the ?? operator features, the less you save in terms of written code?
  2. SimoneB 8/23/2006 10:04 AM
    Gravatar
    Thanks Ryan, interesting. I didn't realize that using parenthesis could let me do assignments. Have you noticed that the more you exploit the ?? operator features, the less you save in terms of written code?
  3. Ryan Farley 8/23/2006 10:47 AM
    Gravatar
    Hi SimoneB,

    Yeah, I suppose it depends on the scenario of where/shy you're using it. I have seen that I don't often save a lot of code, per se, but the code that is produced is easier to read - and that's enough IMO.

    -Ryan
  4. SimoneB 8/23/2006 3:29 PM
    Gravatar
    Sorry for the previous double posting. By the way I agree with you, I like using the new operator too even if sometimes it doean't save much coding.
  5. abhinav 10/11/2006 11:13 AM
    Gravatar
    now the code is cryptic ( elegant?) enough to scare away the non-technically inclined to mess around with your code.

    lets face it - isnt that part of the reason why we loved the ternary operator as well ?
  6. Ryan Farley 10/11/2006 11:23 AM
    Gravatar
    abhinav,

    Excellent point :-p

    I love the syntax and in my mind, as demented as it might be, it makes perfect sense. For me, it simplifies the code as you do not need a bigger if statement when you can say it in simpler terms. If the less-technical see cryptic or scared to touch it, then we might see that as an extra side-benefit too. Hehe.

    -Ryan
  7. minik peri 12/2/2007 2:31 PM
    Gravatar
    Sorry for the previous double posting. By the way I agree with you, I like using the new operator too even if sometimes it doean't save much coding.
  8. minik peri 12/2/2007 2:32 PM
    Gravatar
    Thanks Ryan, interesting. I didn't realize that using parenthesis could let me do assignments. Have you noticed that the more you exploit the ?? operator features, the less you save in terms of written code?
Comments have been closed on this topic.



 

News


Also see my CRM Developer blog

Connect:              


Sponsor

Sections