{"id":3992,"date":"2014-03-30T07:00:03","date_gmt":"2014-03-30T07:00:03","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/code-contracts-in-net-4-0-no-joy-for-non-nullable-reference-types-fans-collection-of-common-programming-errors\/"},"modified":"2014-03-30T07:00:03","modified_gmt":"2014-03-30T07:00:03","slug":"code-contracts-in-net-4-0-no-joy-for-non-nullable-reference-types-fans-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/code-contracts-in-net-4-0-no-joy-for-non-nullable-reference-types-fans-collection-of-common-programming-errors\/","title":{"rendered":"Code Contracts in .NET 4.0, no joy for non-nullable reference types fans?-Collection of common programming errors"},"content":{"rendered":"<p>I think you&#8217;re correct about this. Non-nullable reference checking at compile time was the killer feature I was waiting for in Code Contracts, and it isn&#8217;t really there.<\/p>\n<p>For those wondering what this means, consider an analogy with value types. They were not nullable originally, but now they are if you put a question mark after the type name:<\/p>\n<pre><code>int? n;\n<\/code><\/pre>\n<p>For consistency it would be ideal if the same was true of reference types. But that would break all existing C# programs and so isn&#8217;t an option. In the research language Spec# they went with using an exclamation mark suffix to mean non-nullable:<\/p>\n<pre><code>string! s = \"Hello\";\n<\/code><\/pre>\n<p>As with ordinary value types, the compiler statically checks that a <code>string!<\/code> variable is not used on any code path before it has been initialised (I believe Spec# requires declaration and initialization to occur in the same statement).<\/p>\n<p>It also bans the assignment of <code>null<\/code> to that variable.<\/p>\n<p>And of course, it bans the assignment of an ordinary <code>string<\/code> to a <code>string!<\/code>. So how do bridge the gap between the two kinds of type? By writing a check:<\/p>\n<pre><code>string x = GetStringFromSomewhere();\n\nif (x != null)\n    s = x; \/\/ okay because compiler sees null check\n<\/code><\/pre>\n<p>The sad truth is that the majority of reference variables in most programs are likely to be non-nullable if the program is correct. Nullable variables are in the minority. And yet they are the default.<\/p>\n<p>Another bad idea from the 1960s!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I think you&#8217;re correct about this. Non-nullable reference checking at compile time was the killer feature I was waiting for in Code Contracts, and it isn&#8217;t really there. For those wondering what this means, consider an analogy with value types. They were not nullable originally, but now they are if you put a question mark [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3992","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3992"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3992\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}