{"id":220,"date":"2010-02-13T00:20:58","date_gmt":"2010-02-13T07:20:58","guid":{"rendered":"http:\/\/www.nerdhive.org\/glob\/?p=220"},"modified":"2010-02-13T00:20:58","modified_gmt":"2010-02-13T07:20:58","slug":"more-c-syntactic-sugar","status":"publish","type":"post","link":"http:\/\/www.soleillapierre.ca\/blog\/?p=220","title":{"rendered":"More C# syntactic sugar"},"content":{"rendered":"<p>First up, the &#8220;??&#8221; uperator. You&#8217;ll probably suspect that this is related to the ?= ternary operator, and you&#8217;d be right. The ?? operator is called the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms173224.aspx\" target=\"_blank\">null-coalescing operator<\/a>. Not a very meaningful name, but here&#8217;s what it does.<\/p>\n<div class=\"codecolorer-container cpp vibrant\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"cpp codecolorer\">foo <span class=\"sy1\">=<\/span> bar <span class=\"sy4\">??<\/span> baz<span class=\"sy4\">;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>is equivalent to:<\/p>\n<div class=\"codecolorer-container cpp vibrant\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"cpp codecolorer\">foo <span class=\"sy1\">=<\/span> <span class=\"br0\">&#40;<\/span>bar <span class=\"sy3\">!<\/span><span class=\"sy1\">=<\/span> null<span class=\"br0\">&#41;<\/span> <span class=\"sy4\">?<\/span> bar <span class=\"sy4\">:<\/span> baz<span class=\"sy4\">;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>and also to this, which I usually write because I find the ternary operator ugly:<\/p>\n<div class=\"codecolorer-container cpp vibrant\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/><\/div><\/td><td><div class=\"cpp codecolorer\">foo <span class=\"sy1\">=<\/span> bar<span class=\"sy4\">;<\/span><br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>foo <span class=\"sy1\">==<\/span> null<span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; foo <span class=\"sy1\">=<\/span> baz<span class=\"sy4\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>Also discovered recently: <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb383977.aspx\" target=\"_blank\">extension methods<\/a>. Not having private access is a bit of a drawback, though I can certainly understand why that is. It amounts to a convenient way to write helper code for awkward APIs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First up, the &#8220;??&#8221; uperator. You&#8217;ll probably suspect that this is related to the ?= ternary operator, and you&#8217;d be right. The ?? operator is called the null-coalescing operator. Not a very meaningful name, but here&#8217;s what it does. 1foo = bar ?? baz; is equivalent to: 1foo = &#40;bar != null&#41; ? bar : [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-softwarehacking"],"_links":{"self":[{"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=220"}],"version-history":[{"count":0,"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=\/wp\/v2\/posts\/220\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}