What is the difference between hiding a method and shading in C #?

What is the difference between hiding a method and shading in C #? Are they the same or different? Can we call them polymorphism (compilation time or runtime)?

+4
source share
5 answers

What is the difference between hiding a method and shading in C #?

Shadow is another commonly used term for concealment. The C # specification uses only "hiding", but is acceptable.

You only call the "hide method", but there are forms of hiding other than hiding the method. For instance:

namespace N { class D {} class C { class N { class D { ND nd; // Which ND does this refer to? 

the nested class N hides the namespace N when inside D.

Can they be called polymorphism (compilation time or runtime)?

Hiding the method can be used for polymorphism, yes. You can even mix a method by hiding it with a method override; legally introduce a new virtual method, hiding the old virtual method; in this case, the selected virtual method depends on the type of compilation time and the execution time of the receiver. This is very confusing and you should avoid it if possible.

+16
source

The VB.NET compiler calls it shading, in C # it is called hiding. The challenge of shading it in C # is overflow from VB.

And this is a compiler warning, in fact it is a conflict of names between the base and derived classes.

Can they be called polymorphism (compilation time or runtime)?

This, of course, is not a form of polymorphism at runtime. Calling a hidden or hidden method is allowed at compile time. What makes it generally not called or considered polymorphism.

+4
source

Two words mean the same thing in C #.

Hide method == shadowing.

You can use this as a form of polymorphism - when you do not want the base class method to be visible / useful through the inheritance class.

The shading method is completely separate from the base class - this is a new method. The term "hiding" is used because it has an identical signature with one of the base class and "hides" it - it breaks the inheritance chain.

+2
source

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.

+2
source

Hiding the C # name ( new modifier ) is called shading in VB.NET (the Shadows keyword ).

This can be regarded as polymorphism only in the sense that redefinition is a "polymorphism", that is, static or compilation time. This is not polymorphism in the classical sense of calling virtual functions.

+2
source

Source: https://habr.com/ru/post/1399225/


All Articles