What is the difference between method hiding and shadowing in C#?-Collection of common programming errors

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 “hiding” is related to polymorphism but what is called “shadowing” is not.

In C# parlance, when you say “hiding” you’re usually talking about inheritance, where a more derived method “hides” a base-class method from the normal inherited method call chain.

When you say “shadow” you’re usually talking about scope: an identifier in an inner scope is “shadowing” an identifier at a higher scope. In other languages, what is called “hiding” in C# is sometimes called “shadowing” as well.

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.

public class A
{
  public int B;
  public void C()
  {
    return this.B;
  }
}

public class D : A
{
  public int X;

  public new void C()
  {
    var X = 1.0m;
    return X;
  }
}

Method D.C() “hides” method A.C(); normally, a call to D.C() would always call into the base classes A.C() method, since it’s not virtual. We don’t want that; we want D.C(). Obviously this is something you should try to avoid, because it’s confusing, especially if you start up-casting your D’s to A’s, but it exists if you need it. Also, note that method hiding is automatic: without the new keyword here, D.C() still hides A.C() but we get a warning because usually that’s not what you want. The new keyword just makes it clear that is really is what we want.

Local variable X in D.C() shadows class member D.X within the scope of D.C() only. In this case, there are two things in scope that could legitimately be called X and the compiler needs rules to tell it which one you mean. The “more local” X shadows the “less local” D.X so that’s what we get.