{"id":4724,"date":"2014-03-30T14:59:18","date_gmt":"2014-03-30T14:59:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-best-practice-for-validating-this-argument-in-extension-methods-collection-of-common-programming-errors\/"},"modified":"2014-03-30T14:59:18","modified_gmt":"2014-03-30T14:59:18","slug":"c-best-practice-for-validating-this-argument-in-extension-methods-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-best-practice-for-validating-this-argument-in-extension-methods-collection-of-common-programming-errors\/","title":{"rendered":"C#: Best practice for validating &ldquo;this&rdquo; argument in extension methods-Collection of common programming errors"},"content":{"rendered":"<p>You should throw an ArgumentNullException. You&#8217;re attempting to do argument validation and hence should throw an exception tuned to argument validation. NullReferencException is not an argument validation exception. It&#8217;s a runtime error.<\/p>\n<p>Don&#8217;t forget, extension methods are just static methods under the hood and can be called as such. While it may seem on the surface to make sense to throw a NullReferenceException on an extension method, it does not make sense to do so for a static method. It&#8217;s not possible to determine the calling convention in the method and thus ArgumentException is the better choice.<\/p>\n<p>Also, you should not ever explicitly throw a NullReferenceException. This should only be thrown by the CLR. There are subtle differences that occur when explicitly throwing exceptions that are normally only thrown by the CLR.<\/p>\n<p>This is also close to a dupe of the following<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You should throw an ArgumentNullException. You&#8217;re attempting to do argument validation and hence should throw an exception tuned to argument validation. NullReferencException is not an argument validation exception. It&#8217;s a runtime error. Don&#8217;t forget, extension methods are just static methods under the hood and can be called as such. While it may seem on the [&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-4724","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4724","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=4724"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4724\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}