These are just two different words for the same thing, but they differ in the context where you most often use them. Typically, what is called “hiding” is associated with polymorphism, but what is called “shading” is not.
In C #, when you say “hide,” you usually talk about inheritance, where a more derived method “hides” the base class method from the usual chain of invocation of inherited methods.
When you say “shadow,” you usually talk about scope: the identifier in the inner region “obscures” the identifier in the higher region. In other languages, the so-called "hide" in C # is sometimes called "shading."
Both are compile-time concepts; they describe which object the given identifier refers to in the given context when the compiler goes to its binding.
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; } }
The DC() method hides the AC() method; usually a call to DC() will always call the AC() base class method, since it is not virtual . We do not want this; we want DC() . Obviously, this is something you should try to avoid, because it is confusing, especially if you start discarding your D by A, but it exists if you need it. Also note that the method is hidden automatically: without the new keyword, DC() still hides AC() , but we get a warning, because usually this is not what you want. The new keyword just makes it clear that this is really what we want.
The local variable X in DC() adds only a member of the DX class only within DC() . In this case, there are two things in scope that can legitimately be called X , and the compiler needs rules to tell which one you have in mind. A "more local" X shades the "less local" DX so that we get.
source share