You’re never too old to learn it seems… Today, by reading this entry on Ayende’s blog, I discovered the null–coalescing operator in C# 2.0. I often use code like the following:
string name = (userName == null? “<no name entered>” : userName);
There’s nothing wrong with this code (although in some cases, the null-pattern is a better alternative) but the ternary operator ?: makes the code less readable, and the fact that you have to specify the userName variable twice has always bothered me somewhat. Well, it seems that someone at Microsoft felt the same and decided to do something about it! In C# 2.0 you can now write
string name = userName ?? “<no name>”;
Of course, in SQL that was already possible with the COALESCE function:
set @name = coalesce(userName, “<no name>”)
Cool!
Remark
This operator allows for a very elegant use of the null-pattern:
public User findUser(string name) {
User user;
// insert some highly advanced search algo here
…
// return found user, or if nothing found, the NullUser instance
return user ?? NullUser.Instance;
}
August 11, 2006 at 10:53 am
Actually I think that SQL function allows you to provide variable number of arguments, of it “Returns the first nonnull expression among its arguments”. This is slightly better than the C# ?? operator. To do it in C# you have to create a method accepting variable number of arguments.
August 11, 2006 at 11:10 am
You can use the ?? operator multiple times in a row, as in return a ?? b ?? c ?? d;
August 11, 2006 at 10:15 pm
Wouldn’t this be more “.NET 2.0-ish”:
string[] list = new string[] { null, null, “not null” };
string firstNotNull =
Array.Find(list,
new Predicate(delegate(string item){
return item != null;
}));
March 2, 2011 at 7:16 pm
I know this post is rather old, but when I read valuable information I find it a shame not to thank the author and comment on what’s been said.
This is essentially equivalent to the variable parameter coalesce function available in multiple DBMSs. That is, it’s quite easy to replicate the same functionality.
The evaluation of the LHS expression of the ?? operator returns the evaluated RHS expression in the event that the LHS evaluates to NULL. Since the RHS expression may contain any valid C# expression (to include one containing the ?? operator), you can chain these statements together thus replicating coalesce.
To me, it’s quite readable…but I love the tertiary operator as well.
@Chris: I would say that your approach would be an ideal solution to a problem wherein you aren’t necessarily comparing the evaluation of expressions…rather you are comparing data. The ?? operator’s operands are expressions which aren’t necessarily limited to data.
July 5, 2013 at 9:25 pm
Excellent article! We are linking to this great article on our website.
Keep up the good writing.
March 7, 2014 at 6:04 pm
Quality content is the secret to be a focus for the visitors to pay a visit the web site, that’s what
this site is providing.
June 2, 2014 at 2:29 am
That is very attention-grabbing, You are an overly
professional blogger. I’ve joined your feed and sit up for in quest of
extra of your magnificent post. Also, I’ve shared
your web site in my social networks
June 14, 2014 at 3:36 am
Wonderful goods from you, man. I’ve understand your stuff previous to and you are just too wonderful.
I really like what you’ve acquired here, really like
what you’re stating and the way in which you say it.
You make it entertaining and you still care
for to keep it smart. I can’t wait to read much more from you.
This is actually a great site.
June 14, 2014 at 10:01 pm
Hey just wanted to give you a quick heads up and let you know a few
of the pictures aren’t loading correctly. I’m not sure why but I think its a linking issue.
I’ve tried it in two different browsers and both show the same
outcome.
June 14, 2014 at 11:42 pm
Wow, marvelous blog layout! How long have you been blogging for?
you make blogging look easy. The overall
look of your website is wonderful, as well as the content!
June 17, 2014 at 2:50 am
I like the helpful info you provide on your articles.
I’ll bookmark your blog and take a look at again right here
regularly. I am slightly certain I will be told plenty of new stuff proper here!
Best of luck for the following!
June 18, 2014 at 12:19 am
Greetings! Very helpful advice within this article! It’s
the little changes which will make the largest
changes. Thanks a lot for sharing!
July 8, 2014 at 6:28 am
It’s wonderful that you are getting ideas from this post as well as from our argument made at this time.
August 6, 2014 at 2:03 pm
whoah this weblog is excellent i like reading your articles.
Keep up the good work! You know, lots of individuals are hunting round for
this information, you could help them greatly.
August 7, 2014 at 1:15 am
Different countries have different drunk driving penalties.
The fine this conviction carries ranges from $350 to $1,500, and you can expect to have your license suspended for a period of one to 10 years.
What about driving with a little bit of alcohol in your
system.
August 12, 2014 at 4:35 am
What a material of un-ambiguity and preserveness of valuable knowledge on the topic of unpredicted emotions.
September 2, 2014 at 11:48 pm
I enjoy what you guys are usually up too. This kind of clever work and coverage!
Keep up the good works guys I’ve included you guys to blogroll.
September 3, 2014 at 12:25 am
Incredible quest there. What occurred after?
Thanks!
September 8, 2014 at 1:22 am
I like the helpful information you provide in your articles.
I will bookmark your blog and check again here frequently.
I’m quite certain I’ll learn plenty of new stuff right here!
Good luck for the next!
September 30, 2014 at 12:20 am
I have read so many articles or reviews regarding the blogger lovers however this piece
of writing is really a nice piece of writing,
keep it up.
October 1, 2014 at 11:56 pm
Buddha: Water features that position a ‘wealth deity’ at the top, such as a laughing
Buddha, are considered to bring good luck
and stability of wealth. Push field mowers are greatly preferred by countless people.
We have made it a point to make our inventory quickly accessible
to the consumer without requiring advanced computer skills.
Maybe you won’t be going off-grid, but you can make a difference and reduce your reliance on energy wasting, pollution producing methods of living.
October 2, 2014 at 6:53 am
Nice post. I was checking continuously this blog and
I am impressed! Very useful info particularly the final phase :
) I take care of such information much. I was looking for this certain info for a
very long time. Thanks and best of luck.
December 31, 2014 at 9:27 pm
I used to be recommended this blog by way of my cousin. I’m
not certain whether or not this post is written by way of him as nobody else understand
such distinct approximately my trouble. You’re wonderful!
Thank you!
January 28, 2015 at 2:44 pm
I always spent my half an hour to read this blog’s articles or reviews all the time along with a
cup of coffee.
March 4, 2015 at 9:05 pm
What’s up, just wanted to mention, I liked this blog post.
It was helpful. Keep on posting!
January 16, 2016 at 12:55 pm
sites of interest iam happy linked to this place stick with it this really answered my problem
April 4, 2020 at 9:57 am
Hello to all
In this baffling continuously, I love you all
Appreciate your one’s nearest and friends
October 21, 2020 at 10:49 pm
navigate to this site кардинг форум