Search Options
close
Search the following clips:
All Clips
news
science
politics
food
economy
art
technology
health
internet
religion
psychology
Sign Up
Install
Learn More
Login
Coalesce operator in C# 2.0
fmcypriano
follow
0
8-10-2006 7:49 AM
211 views
tags:
c#
,
.net
,
coalesce
,
sql
Add a Comment
Login
to Comment. Not a member yet?
Sign up
Today's Top Clips
Cute Animal Photos
Rare baby albino dolphin
Ist Ever Supernova Fireball Observed
Climategate: You should be steamed
Grow Your Own Teeth Replacements
Bee Hive In A Bell Jar
The True Story of the Statue of Liberty
Thousands Protest Global Warming
Plus size models are really flaunting it!
Animal Magnetism
visit the
Top Clips page
View the Top Clips from
August 10, 2006
Embed This Clip In Your Site...
<div style="margin: 12px 0px; font-family: arial; color: #333333; background: #ffffff; border: solid 4px #e5e5e5; width: 100%; clear: left;"><div class="CM_CTB_Content_Wrap" style="margin: 0px; padding: 0px;background-color: #ffffff;"><div style="border-bottom: solid 1px #dcdcdc; white-space: nowrap; margin-bottom: 8px; background-color: #eeeeee ;background-image: url(http://clipmarks.com/images/source-bg.gif); background-repeat: repeat-x; height: 24px; line-height: 24px; vertical-align: middle; padding-bottom: 4px; color: #666666; font-size: 10px;" ><a href="http://clipmarks.com/clip-to-blog/" title="see clips that are hot right now"><img src="http://content.clipmarks.com/blog_embed/6550db60-f638-4522-b83a-37a428244493/957CA78B-6B1A-4F49-A91C-1D2B42947CFB/" alt="" width="19" height="19" border="0" style="vertical-align: middle; margin: 0px 4px; display: inline; border: none; float:none;" /></a>clipped from <a title="http://onlytalkingsense.wordpress.com/2006/07/07/coalesce-goes-c/" href="http://onlytalkingsense.wordpress.com/2006/07/07/coalesce-goes-c/" style="font-size: 11px;">onlytalkingsense.wordpress.com</a></div><blockquote style="text-align: left; padding: 0px 8px; margin: 4px 0px 8px 0px; background: transparent; border: none;" cite="http://onlytalkingsense.wordpress.com/2006/07/07/coalesce-goes-c/"><FONT size="2" face="Arial">There’s nothing wrong with this code (although in some cases, the <A href="http://www.cs.oberlin.edu/%7Ejwalker/nullObjPattern/" target="_blank">null-pattern</A> 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! I</FONT></blockquote><div style="height: 2px; font-size: 2px; background: #dcdcdc; border-bottom: solid 1px #f5f5f5; margin: 2px 4px;"></div><blockquote style="text-align: left; padding: 0px 8px; margin: 4px 0px 8px 0px; background: transparent; border: none;" cite="http://onlytalkingsense.wordpress.com/2006/07/07/coalesce-goes-c/"><P><FONT size="2" face="Courier%20New"> string name = userName ?? “<no name>”;</FONT></P></blockquote><div style="height: 2px; font-size: 2px; background: #dcdcdc; border-bottom: solid 1px #f5f5f5; margin: 2px 4px;"></div><blockquote style="text-align: left; padding: 0px 8px; margin: 4px 0px 8px 0px; background: transparent; border: none;" cite="http://onlytalkingsense.wordpress.com/2006/07/07/coalesce-goes-c/"><P><FONT size="2" face="Courier%20New"> set @name = coalesce(userName, “<no name>”)</FONT></P></blockquote><div style="height: 2px; font-size: 2px; background: #dcdcdc; border-bottom: solid 1px #f5f5f5; margin: 2px 4px;"></div><blockquote style="text-align: left; padding: 0px 8px; margin: 4px 0px 8px 0px; background: transparent; border: none;" cite="http://onlytalkingsense.wordpress.com/2006/07/07/coalesce-goes-c/"><P><FONT size="2" face="Arial">Of course, in SQL that was already possible with the COALESCE function:</FONT></P></blockquote><div style="height: 2px; font-size: 2px; background: #dcdcdc; border-bottom: solid 1px #f5f5f5; margin: 2px 4px;"></div><blockquote style="text-align: left; padding: 0px 8px; margin: 4px 0px 8px 0px; background: transparent; border: none;" cite="http://onlytalkingsense.wordpress.com/2006/07/07/coalesce-goes-c/"><P><FONT size="2" face="Courier%20New"> 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; }</FONT> </P></blockquote></div><div style="margin: 0px 6px 6px 4px;"><table style="font-size: 11px;border-spacing: 0px;padding: 0px;" cellpadding="0" cellspacing="0" width="100%"><tr><td style="background:transparent;border-width:0px;padding:0px;"> </td><td align="right" style="background:transparent;border-width:0px;padding:0px;width:107px" width="107"><a href="http://clipmarks.com/share/957CA78B-6B1A-4F49-A91C-1D2B42947CFB/blog/" title="blog or email this clip"><img src="http://content7.clipmarks.com/images/c2b-foot.png" border="0" alt="blog it" width="107" height="17" style="border-width:0px;padding:0px;margin:0px;" /></a></td></tr></table></div></div>
New from the makers of Clipmarks:
Amplify.com - Don't just share the news...Amplify it!
Clipmarks
Home
New Clips
Top Clips
Dashboard
Popular Topics
News
Life
Science
Technology
Entertainment
Get Started
Sign Up
Install Clipping Tool
How Clipping Works
Clip-to-Blog™
ClipSearch
Tools and Resources
FAQ
ClipWeek
Top Clippers
Top Tags
Site Map
About Clipmarks
About Us
Contact
Copyright
Privacy
EULA
OK