Miscellaneous Debris

Avner Kashtan's Frustrations and Exultations

A new conditional assignment operator??

This one may have slipped beneath the radar, with all the new .NET 2.0 and WinFX and other improvements and APIs and language changes. It appears that C# 2.0 adds a new operator to the mix, and I never even knew it was there.

The operator is ??, and it's a conditional non-null operator. Since that doesn't really mean anything, I'll simply and say that the ?? operator returns the left hand operand if it is non-null, otherwise it returns the righthand operand:

string a =  null;
string b = "String";
Console.WriteLine (a ?? b); // Will output "String".


This is shorthand for the common pattern we see using the ternary conditional operator, like this:

Console.WriteLine (a != null ? a : b);

which is in turn shorthand for:

if (a != null)
{
   Console.WriteLine(a);
}
else
{
   Console.WriteLine(b);
}


The ternary conditional is held by many to be a sin against nature and code readability, though I personally find it quite clear and convenient. The non-null conditional is a bit less clear - there's nothing in its syntax to suggest an either/or relation. Perhaps it will clear things up. Perhaps it will lie unused and forgotten . Time will tell.

Note: For some reason, the MSDN library groups the ?? operator with the Assignment operators, rather than the Conditional operators. Strange.
Posted: May 04 2006, 12:40 PM by AvnerK | with 8 comment(s)
Filed under: , ,

Comments

Hugo said:

Small correction:

Console.WriteLine (a != null ? a : b);
# May 4, 2006 11:29 AM

Avner Kashtan said:

*nonchalant whistling*

Of course, that's what I originally wrote. I wouldn't make such a silly mistake, would I? :)

Thanks. Fixed.
# May 4, 2006 11:32 AM

Miscellaneous Debris said:

I remember discovering C#'s null coalescing operator a while ago. I can't say I use it a lot , but it

# June 3, 2007 4:23 AM

Miscellaneous Debris said:

I remember discovering C#'s null coalescing operator a while ago. I can't say I use it a lot , but it's

# June 3, 2007 4:23 AM

poori said:

Im buying my iPhone from a friend, how much does it cost to reactivate it to at&t under my name ?

________________

<a href=unlockiphone22.com/>click here</a>

# November 12, 2009 10:38 PM

LilsWaydaysig said:

Hramos milusto http://nesdjusi.op

# December 30, 2009 7:15 PM

deephotadia said:

DUBLIN (Reuters) – The Irish <a href=http://www.redcarpetflorist.com>Flower Delivery Service</a> Aviation Authorization said it would allow flights to pick up where one progressive sour from all Irish airports from 1200 GMT on Tuesday but volcanic ash could create on more disruptions later in the week and periodically in every nook the summer.

The IAA had closed airports from 0600 GMT until 1200 GMT correct to chance of ash ingestion in aircraft engines, although overflights of Ireland from Britain and continental Europe had not been banned.

# May 4, 2010 7:03 AM

Meatheangeree said:

unhealthy  <a href="http://kaiser-permanente-p.groupsite.com">kaiser permanente news</a> searches  

# November 24, 2010 3:27 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)