Alex Rivera | Logout

Difference between shadowing and overriding in C#?

Asked 2008-12-25T10:45:53.133
110

What's difference between shadowing and overriding a method in C#?

c#
Edit
Report

1 Answer

34

Shadowing is actually VB parlance for what we would refer to as hiding in C#.

Often hiding (shadowing in VB) and overriding are shown as in answer by Stormenet.

A virtual method is shown to be overridden by a sub-class and calls to that method even on the super-class type or from inside code of the super-class will call the replacement implementation from the sub-class.

Then a concrete method is shown (one not marked as virtual or abstract) being hidden by using the new keyword when defining a method with an identical signature on the sub-class. In this case when the method is called on the super-class type the original implementation is used, the new implementation is only available on the sub-class.

However what is often missed is that it is also possible to hide a virtual method.

class A
{
    public virtual void DoStuff() { // original implementation }
}

class B : A
{
    public new void DoStuff() {  //new implementation }
}

B b = new B();
A a = b;

b.DoStuff(); //calls new implementation
a.DoStuff(); //calls original implementation.

Note in the above example DoStuff becomes concrete and can not be overriden. However it is also possible to use both the virtual and new keywords together.

class A
{
    public virtual void DoStuff() { // original implementation }
}

class B : A
{
    public new virtual void DoStuff() {  //new implementation }
}

class C : B
{
    public override void DoStuff() { //replacement implementation }
}

C c = new C();
B b = c;
A a = b;

c.DoStuff(); //calls replacement implementation
b.DoStuff(); //calls replacement implementation
a.DoStuff(); //calls original implementation.

Note that despite all the methods involved being virtual, the override on C does not affect the virtual method on A because of the use of new in B hid

answered 2008-12-25T12:21:36.483

Your Answer