{"id":12,"date":"2008-02-11T15:11:14","date_gmt":"2008-02-11T20:11:14","guid":{"rendered":"http:\/\/www.peteonsoftware.com\/?p=12"},"modified":"2024-03-02T17:31:35","modified_gmt":"2024-03-02T22:31:35","slug":"code-tip-c-coalesce","status":"publish","type":"post","link":"https:\/\/www.peteonsoftware.com\/index.php\/2008\/02\/11\/code-tip-c-coalesce\/","title":{"rendered":"Code Tip: C# Coalesce"},"content":{"rendered":"<p>I have to admit something.  I&#8217;m something of a Sql Coalesce() fan.  I know that Sql Server has an IsNull() function, but there are two bad things about it.  First, it is proprietary to Sql Server and I like to try to write my Sql as close to ANSI as I can so that I don&#8217;t build up too many bad habits that cause me to have problems when I have to work in other DB platforms.  Secondly, IsNull() only takes one option (well, you could nest your IsNull() statements, but that is unwieldy).  Coalesce() lets you give a list of items and just takes the first one of them to not be null.  Apparently, IsNull() <a href=\"http:\/\/weblogs.sqlteam.com\/mladenp\/articles\/2937.aspx\">is also slower<\/a>.  All hail, Coalesce()!!!<\/p>\n<p>Several months ago, I was doing some coding in C# and I was actually wishing that C# had a Coalesce function so that I could not have to do a bunch of manual null checking in my code.  On a whim, I decided to Google C# Coalesce and see if someone had written one or if there was one hidden in the framework somewhere.  To my surprise, I found that C# did have such an animal.  Here is an example of its use.<\/p>\n<pre>\r\n\/\/ Checks the HTML Form for a value,\r\n\/\/ if that wasn't submitted, use the value\r\n\/\/ potentially set elsewhere in the code,\r\n\/\/ if that is null, set the variable to\r\n\/\/ a blank string.\r\nstring value = Request.Form[\"someField\"] as string ?? someVariable ?? string.Empty;\r\n<\/pre>\n<p>I can&#8217;t tell you how much I love having this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have to admit something. I&#8217;m something of a Sql Coalesce() fan. I know that Sql Server has an IsNull() function, but there are two bad things about it. First, it is proprietary to Sql &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[89],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","category-code-tips","tag-code-tips"],"_links":{"self":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":0,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}