{"id":96676,"date":"2017-07-25T07:00:00","date_gmt":"2017-07-25T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=96676"},"modified":"2019-03-13T01:14:28","modified_gmt":"2019-03-13T08:14:28","slug":"20170725-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170725-00\/?p=96676","title":{"rendered":"If there is no difference between two options, choose the one that is easier to debug"},"content":{"rendered":"<p>In C# you have two ways of casting one object to another. One is to use the <code>as<\/code> operator, which attempts to casting the object and either returns the successfully cast result or returns <code>null<\/code>. <\/p>\n<p>Another way is to use the casting operator. <\/p>\n<p>In the case where you are going to use the result of the cast immediately, Which should you use? <\/p>\n<pre>\n\/\/ option 1\nvar thing = GetCurrentItem();\nvar foo = thing as Foo;\nfoo.DoSomething();\n\n\/\/ option 2\nvar thing = GetCurrentItem();\nvar foo = (Foo)thing;\nfoo.DoSomething();\n<\/pre>\n<p>Now, suppose the <code>thing<\/code> is not a <code>Foo<\/code> after all. Both options will crash, but they will crash differently. <\/p>\n<p>In the first version, you will crash with a <code>Null&shy;Reference&shy;Exception<\/code> at the <code>foo.<\/code><code>Do&shy;Something()<\/code>, and the crash dump will confirm that, yes, the <code>foo<\/code> variable is null. But the <code>thing<\/code> might not be in the crash dump. Maybe the crash dump captures only the parameters that participated in the expression that resulted in the exception. Or maybe <code>thing<\/code> was GC&#8217;d away. You can&#8217;t tell whether the problem is that <code>Get&shy;Current&shy;Item<\/code> returned <code>null<\/code>, or that <code>Get&shy;Current&shy;Item<\/code> returned an object that wasn&#8217;t a <code>Foo<\/code>. And if it wasn&#8217;t a <code>Foo<\/code>, what was it? <\/p>\n<p>In the second version, there are two ways the code could crash. If the <code>thing<\/code> is null, then you will get a <code>Null&shy;Reference&shy;Exception<\/code> at the <code>foo.<\/code><code>Do&shy;Something()<\/code>. But if the <code>thing<\/code>  is the wrong kind of object, then the crash will occur at the point of the cast with an <code>Invalid&shy;Cast&shy;Exception<\/code>. And if you&#8217;re lucky, the debugger will show you the thing that could not be cast. Even if it doesn&#8217;t, you can at least determine from the type of the exception which of the two cases you&#8217;re in. <\/p>\n<p><b>Exercise<\/b>: The following two lines of code are functionally equivalent. Which is easier to debug? <\/p>\n<pre>\n\/\/ option 1\ncollection.FirstOrDefault().DoSomething();\n\n\/\/ option 2\ncollection.First().DoSomething();\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Because you&#8217;re going to be debugging it eventually.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-96676","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Because you&#8217;re going to be debugging it eventually.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=96676"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96676\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=96676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=96676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=96676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}