{"id":44,"date":"2010-03-05T08:35:51","date_gmt":"2010-03-05T07:35:51","guid":{"rendered":"http:\/\/blog.blaten.nl\/?p=44"},"modified":"2010-03-05T08:35:51","modified_gmt":"2010-03-05T07:35:51","slug":"defaultt-vs-null","status":"publish","type":"post","link":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2010\/03\/05\/defaultt-vs-null\/","title":{"rendered":"default(T) vs null"},"content":{"rendered":"<p>When programming in C#, we often test if a variable is used in the following way:<br \/>\n<img decoding=\"async\" style=\"border: 0pt none; width: 0pt; height: 0pt; display: none;\" src=\"http:\/\/tokentracker.com\/token.gif?id=52A9a1cb9\" alt=\"\" \/><\/p>\n<pre class=\"syntax csharp\">if (var != null) {\n  \/\/ do the right thing\n}\nelse\n{\n  \/\/ fallback or raise exception\n}<\/pre>\n<p>This works well if the variable is a nullable type or a reference type, but when the variable is of a non nullable type it will raise an exception. This also works the other way around, when you test a reference typed by comparing it to 0, chances are quite big the runtime will also raise an exception.<\/p>\n<p>Now you&#8217;re probably thinking it&#8217;s impossible to do this, because the compiler will warn you about this and it simply won&#8217;t compile. True, except when we don&#8217;t know the type beforehand, which usually is the case when using generics.<\/p>\n<p>Take a look at this code:<\/p>\n<pre class=\"syntax csharp\">public bool isValueSet&lt;T&gt;(T source)\n{\n  return source != null;\n}<\/pre>\n<p>This piece of code works almost exactly as the first code fragment in this post, though here it has room for the aforementioned caveat. We don&#8217;t know if the type T is a reference type or a value-type. There are more ways to make it work exactly how it&#8217;s intended.<\/p>\n<ol>\n<li>Setup a control flow which switches between value-types and other types (reference types) using the GetType().IsValueType property<\/li>\n<li>Use the <strong>default(T)<\/strong> construction, so the framework will automatically assigns the default value according to the given type. I myself prefer this solution.<\/li>\n<\/ol>\n<p>When using the default(T) following code should always work as expected:<\/p>\n<pre class=\"syntax csharp\">public bool isValueSet&lt;T&gt;(T source)\n{\n  return source != default(T);\n}<\/pre>\n<p>long live the framework \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When programming in C#, we often test if a variable is used in the following way: if (var != null) { \/\/ do the right thing } else { \/\/ fallback or raise exception } This works well if the variable is a nullable type or a reference type, but when the variable is of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7,1],"tags":[4],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>default(T) vs null &raquo; Christiaan Nieuwlaat .nl<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2010\/03\/05\/defaultt-vs-null\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"default(T) vs null &raquo; Christiaan Nieuwlaat .nl\" \/>\n<meta property=\"og:description\" content=\"When programming in C#, we often test if a variable is used in the following way: if (var != null) { \/\/ do the right thing } else { \/\/ fallback or raise exception } This works well if the variable is a nullable type or a reference type, but when the variable is of [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2010\/03\/05\/defaultt-vs-null\/\" \/>\n<meta property=\"og:site_name\" content=\"Christiaan Nieuwlaat .nl\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/christiaan.nieuwlaat\" \/>\n<meta property=\"article:published_time\" content=\"2010-03-05T07:35:51+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/tokentracker.com\/token.gif?id=52A9a1cb9\" \/>\n<meta name=\"author\" content=\"chris\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@krizzje\" \/>\n<meta name=\"twitter:site\" content=\"@krizzje\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"chris\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2010\/03\/05\/defaultt-vs-null\/\",\"url\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2010\/03\/05\/defaultt-vs-null\/\",\"name\":\"default(T) vs null &raquo; Christiaan Nieuwlaat .nl\",\"isPartOf\":{\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#website\"},\"datePublished\":\"2010-03-05T07:35:51+00:00\",\"dateModified\":\"2010-03-05T07:35:51+00:00\",\"author\":{\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2010\/03\/05\/defaultt-vs-null\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2010\/03\/05\/defaultt-vs-null\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2010\/03\/05\/defaultt-vs-null\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.christiaannieuwlaat.nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"default(T) vs null\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#website\",\"url\":\"https:\/\/www.christiaannieuwlaat.nl\/\",\"name\":\"Christiaan Nieuwlaat .nl\",\"description\":\"IT Whizzkid behind the wheels of steel\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.christiaannieuwlaat.nl\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef\",\"name\":\"chris\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g\",\"caption\":\"chris\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"default(T) vs null &raquo; Christiaan Nieuwlaat .nl","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2010\/03\/05\/defaultt-vs-null\/","og_locale":"en_US","og_type":"article","og_title":"default(T) vs null &raquo; Christiaan Nieuwlaat .nl","og_description":"When programming in C#, we often test if a variable is used in the following way: if (var != null) { \/\/ do the right thing } else { \/\/ fallback or raise exception } This works well if the variable is a nullable type or a reference type, but when the variable is of [&hellip;]","og_url":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2010\/03\/05\/defaultt-vs-null\/","og_site_name":"Christiaan Nieuwlaat .nl","article_publisher":"https:\/\/www.facebook.com\/christiaan.nieuwlaat","article_published_time":"2010-03-05T07:35:51+00:00","og_image":[{"url":"http:\/\/tokentracker.com\/token.gif?id=52A9a1cb9"}],"author":"chris","twitter_card":"summary_large_image","twitter_creator":"@krizzje","twitter_site":"@krizzje","twitter_misc":{"Written by":"chris","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2010\/03\/05\/defaultt-vs-null\/","url":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2010\/03\/05\/defaultt-vs-null\/","name":"default(T) vs null &raquo; Christiaan Nieuwlaat .nl","isPartOf":{"@id":"https:\/\/www.christiaannieuwlaat.nl\/#website"},"datePublished":"2010-03-05T07:35:51+00:00","dateModified":"2010-03-05T07:35:51+00:00","author":{"@id":"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef"},"breadcrumb":{"@id":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2010\/03\/05\/defaultt-vs-null\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.christiaannieuwlaat.nl\/index.php\/2010\/03\/05\/defaultt-vs-null\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2010\/03\/05\/defaultt-vs-null\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.christiaannieuwlaat.nl\/"},{"@type":"ListItem","position":2,"name":"default(T) vs null"}]},{"@type":"WebSite","@id":"https:\/\/www.christiaannieuwlaat.nl\/#website","url":"https:\/\/www.christiaannieuwlaat.nl\/","name":"Christiaan Nieuwlaat .nl","description":"IT Whizzkid behind the wheels of steel","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.christiaannieuwlaat.nl\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef","name":"chris","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g","caption":"chris"}}]}},"_links":{"self":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/posts\/44"}],"collection":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":0,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}