{"id":4411,"date":"2014-03-30T10:43:54","date_gmt":"2014-03-30T10:43:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/what-is-the-difference-between-method-hiding-and-shadowing-in-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T10:43:54","modified_gmt":"2014-03-30T10:43:54","slug":"what-is-the-difference-between-method-hiding-and-shadowing-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/what-is-the-difference-between-method-hiding-and-shadowing-in-c-collection-of-common-programming-errors\/","title":{"rendered":"What is the difference between method hiding and shadowing in C#?-Collection of common programming errors"},"content":{"rendered":"<p>They are just two different words for the same thing, but differ in the context where you most often use them. Typically, what is called &#8220;hiding&#8221; is related to polymorphism but what is called &#8220;shadowing&#8221; is not.<\/p>\n<p>In C# parlance, when you say &#8220;hiding&#8221; you&#8217;re usually talking about inheritance, where a more derived method &#8220;hides&#8221; a base-class method from the normal inherited method call chain.<\/p>\n<p>When you say &#8220;shadow&#8221; you&#8217;re usually talking about scope: an identifier in an inner scope is &#8220;shadowing&#8221; an identifier at a higher scope. In other languages, what is called &#8220;hiding&#8221; in C# is sometimes called &#8220;shadowing&#8221; as well.<\/p>\n<p>Both are compile-time concepts; they describe what object a given identifier refers to in a given context when the compiler goes to bind it.<\/p>\n<pre><code>public class A\n{\n  public int B;\n  public void C()\n  {\n    return this.B;\n  }\n}\n\npublic class D : A\n{\n  public int X;\n\n  public new void C()\n  {\n    var X = 1.0m;\n    return X;\n  }\n}\n<\/code><\/pre>\n<p>Method <code>D.C()<\/code> &#8220;hides&#8221; method <code>A.C()<\/code>; normally, a call to <code>D.C()<\/code> would always call into the base classes <code>A.C()<\/code> method, since it&#8217;s not <code>virtual<\/code>. We don&#8217;t want that; we want <code>D.C()<\/code>. Obviously this is something you should try to avoid, because it&#8217;s confusing, especially if you start up-casting your D&#8217;s to A&#8217;s, but it exists if you need it. Also, note that method hiding is automatic: without the <code>new<\/code> keyword here, <code>D.C()<\/code> still hides <code>A.C()<\/code> but we get a warning because <em>usually<\/em> that&#8217;s not what you want. The <code>new<\/code> keyword just makes it clear that is really is what we want.<\/p>\n<p>Local variable <code>X<\/code> in <code>D.C()<\/code> shadows class member <code>D.X<\/code> within the scope of <code>D.C()<\/code> only. In this case, there are two things in scope that could legitimately be called <code>X<\/code> and the compiler needs rules to tell it which one you mean. The &#8220;more local&#8221; <code>X<\/code> shadows the &#8220;less local&#8221; <code>D.X<\/code> so that&#8217;s what we get.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>They are just two different words for the same thing, but differ in the context where you most often use them. Typically, what is called &#8220;hiding&#8221; is related to polymorphism but what is called &#8220;shadowing&#8221; is not. In C# parlance, when you say &#8220;hiding&#8221; you&#8217;re usually talking about inheritance, where a more derived method &#8220;hides&#8221; [&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-4411","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4411","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=4411"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4411\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}