{"id":2355,"date":"2014-09-15T19:33:37","date_gmt":"2014-09-15T19:33:37","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2355"},"modified":"2014-09-15T19:36:50","modified_gmt":"2014-09-15T19:36:50","slug":"comparing-a-generic-to-its-default-value","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/comparing-a-generic-to-its-default-value\/","title":{"rendered":"Comparing a generic to its default value"},"content":{"rendered":"<p>This is a very short post on something which, for some reason, I keep forgetting and having to find code I&#8217;ve written in the past to refresh my memory &#8211; so I thought I&#8217;d write a quick post to remind myself about this.<\/p>\n<p>So you have a generic type T. Maybe it&#8217;s passed as an argument to a method and you want to check whether it&#8217;s set to its default value (in the case of classes this is equivalent to null).<\/p>\n<p>To do this we use the following code (where o is the variable name of some generic type)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nif(EqualityComparer&lt;T&gt;.Default.Equals(o, default(T))) \r\n{\r\n   \/\/ do something\r\n}\r\n<\/pre>\n<p>As previously stated &#8211; for class types, this is equivalent to null, for other types it depends upon their default value.<\/p>\n<p>This code will handle structs and classes as well as Nullable types and also avoids boxing (see <a href=\"http:\/\/books.google.co.uk\/books?id=t1de8nSVYnkC&#038;pg=PT492&#038;lpg=PT492&#038;dq=EqualityComparer%3CT%3E.Default+avoiding+the+boxing&#038;source=bl&#038;ots=25ndVpPj8y&#038;sig=Lu5C2l_ohqlmVmnpGGPfXmcHnGw&#038;hl=en&#038;sa=X&#038;ei=StESVNLMEsLUaueBgZAF&#038;ved=0CD0Q6AEwBA#v=onepage&#038;q=EqualityComparer%3CT%3E.Default%20avoiding%20the%20boxing&#038;f=false\" title=\"C# 5.0 in a Nutshell\" target=\"_blank\">C# 5.0 in a Nutshell<\/a>).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a very short post on something which, for some reason, I keep forgetting and having to find code I&#8217;ve written in the past to refresh my memory &#8211; so I thought I&#8217;d write a quick post to remind myself about this. So you have a generic type T. Maybe it&#8217;s passed as an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2355","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=2355"}],"version-history":[{"count":7,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2355\/revisions"}],"predecessor-version":[{"id":2368,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2355\/revisions\/2368"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}