{"id":254,"date":"2005-12-08T16:59:44","date_gmt":"2005-12-08T23:59:44","guid":{"rendered":"http:\/\/jameskovacs.com\/2005\/12\/08\/Default+Values+And+In+C+20"},"modified":"2005-12-08T16:59:44","modified_gmt":"2005-12-08T23:59:44","slug":"default-values-and-in-c-20","status":"publish","type":"post","link":"https:\/\/www.jameskovacs.com\/index.php\/2005\/12\/08\/default-values-and-in-c-20\/","title":{"rendered":"Default values and ?? in C# 2.0"},"content":{"rendered":"<p><P>No, I&#8217;m not confused. Read on and all shall become clear&#8230;<\/P><br \/>\n<P>Since the dawn of time*, the conditional-expression operator, ?:,&nbsp;has confused C-language newbies for generations. Not to be outdone, C# 2.0 has introduced the ?? operator. This is a little known addition to the C# language that has an interesting use &#8211; besides confusing VB developers, that is. Consider the following code:<\/P><br \/>\n<P><SPAN style=\"FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\"><SPAN style=\"FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">if<\/SPAN>(title <FONT color=#ff0000>!<\/FONT><SPAN style=\"FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">=<\/SPAN> <SPAN style=\"FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">null<\/SPAN>) {<BR><SPAN style=\"FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">&nbsp;&nbsp; return<\/SPAN> title;<BR>} <SPAN style=\"FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">else<\/SPAN> {<BR><SPAN style=\"FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">&nbsp;&nbsp; return<\/SPAN> string.Empty;<BR>}<\/SPAN><\/P><br \/>\n<P>Basically you want to return a default value if your reference is null. If your type happens to be a string, this could be a shared instance (such as string.Empty) or &#8220;Default&nbsp;Value&#8221; or something else. Other reference types (or Nullables) could do something else.<\/P><br \/>\n<P>This isn&#8217;t hard code, but it is a fair amount of typing. So many would shorten it to:<\/P><br \/>\n<P><SPAN style=\"FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\"><SPAN style=\"FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">return<\/SPAN> (title <FONT color=#ff0000>!<SPAN style=\"FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">=<\/SPAN><\/FONT> <SPAN style=\"FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">null<\/SPAN>) ? title : <SPAN style=\"FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">string<\/SPAN>.Empty;<\/SPAN><\/P><br \/>\n<P>Checking for null returns is fairly common, especially in database work or when reading from config files. So C# 2.0 introduced the ?? operator:<\/P><br \/>\n<P><SPAN style=\"FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\"><SPAN style=\"FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">return<\/SPAN> title ?? <SPAN style=\"FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">string<\/SPAN>.Empty;<\/SPAN><\/P><br \/>\n<P>This does exactly the same thing as the previous two examples, just with less typing. Another interesting use is when working with Nullables:<\/P><br \/>\n<P><SPAN style=\"FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\"><SPAN style=\"FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">int<\/SPAN>? x <SPAN style=\"FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">=<\/SPAN> ReadFromConfig();<BR><SPAN style=\"FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">\/\/ Do some work<\/SPAN><BR><SPAN style=\"FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">int<\/SPAN> y <SPAN style=\"FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent\">=<\/SPAN> x ?? 42;<BR><\/SPAN><\/P><br \/>\n<P>It will take awhile to get used to, but is an interesting addition to the C# toolbox.<\/P><br \/>\n<P>*&nbsp;The dawn of time is roughly sometime in the early 1970s.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>No, I&#8217;m not confused. Read on and all shall become clear&#8230; Since the dawn of time*, the conditional-expression operator, ?:,&nbsp;has confused C-language newbies for generations. Not to be outdone, C# 2.0 has introduced the ?? operator. This is a little known addition to the C# language that has an interesting use &#8211; besides confusing VB [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-254","post","type-post","status-publish","format-standard","hentry","category-dotnetgeneral"],"_links":{"self":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts\/254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/comments?post=254"}],"version-history":[{"count":0,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts\/254\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/media?parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/categories?post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/tags?post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}