Dennis van der Stelt

If you have one problem and use cache to solve it, you now have two problems.

Community

Email Notifications

News

  • Addicted to Refactor! Pro

I read...

I Use...

Tags

Recent Posts

Archives

Blog Subscription Form

  • Email Notifications
    Go

Advanced usage of the ?? operator

P.J. van de Sande from the (Dutch) weblog Born 2 Code .NET has two posts about the in .NET 2.0 new ?? operator. In the first post he explains what it does and why he likes it. After some comments, he decided to write another post about it, trying to explain even further why he likes it so much. But now he pulls some code from his sleeve I really had to take a good look at.

public Brush BackgroundBrush

{

  get

  {

    return _backgroundBrush ??

    (

        _backgroundBrush = GetBackgroundBrushDefault()

    );

  }

}

You have to know how this is evaluated to understand it. Before it returns anything, it first evaluates the ?? operator, which is probably understood much better by the following code block.

public Brush BackgroundBrush

{

  get

  {

    if (_backgroundBrush == null)

    {

      _backgroundBrush = GetBackgroundBrushDefault();

    }

 

    return _backgroundBrush;

  }

}

But still, it's some lovely code. You decide for yourself if you want to see code like that in your project. Perhaps Jan Schreuder would be more happy to see code like this instead of the code in his latest post! ;-)

Comments

Ramon Smits said:

This makes use of the construction that each statement has a return value.

int a,b,c;

a=b=c=6;

Now all three value types will be 6 :)

Just excellent c behaviour that we have inherited.

# August 15, 2006 8:23 AM

Ramon Smits said:

Or like copying stream data into another.

static void Copy(Stream source, Stream destination)

{

■■■const long Size = 1024;

■■■byte[] buffer = new byte[Size];

■■■long readBytes;

■■■while(0<(readBytes=source.Read(buffer,0,readBytes))

■■■{

■■■■■■destination.Write(buffer,0,readBytes);

■■■}

}

ps. This code could contain errors as I just typed it in by hand.

# August 15, 2006 8:29 AM

Paul Gielens said:

With a background in C++ ?? makes perfect sense.
# August 15, 2006 11:09 AM

Jason Haley said:

# August 15, 2006 7:02 PM

Remco van Dalen said:

eeeeeeeeeeeeeeeeuuch !!! What total retard came up with the idea that it was necessary to shorten 6 characters (==null) to 2 characters(??). And what's even worse, it looks like the statement doesn't go between the normal {} but between (). I'm boycotting this thing for life.
# August 17, 2006 12:35 AM

Dennis van der Stelt said:

Remco, thank you for your objective comment! :D

I'll go contact Blizzard if they can add this most beautifull ?? operator into the World of Warcraft scripting language! That'll be the way to get you over the edge! :D

# August 17, 2006 1:01 AM

{ public virtual blog; } said:

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

# August 22, 2006 2:21 PM

{ public virtual blog; } said:

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

# August 22, 2006 2:53 PM

TrackBack said:

# August 23, 2006 6:05 AM

JanellHess22 said:

This is good that we are able to take the <a href="http://lowest-rate-loans.com">loans</a> moreover, that opens completely new possibilities.

# May 8, 2010 3:24 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 3 and 1 and type the answer here: