You normally use overriding in the following cases:
- You are defining polymorphic derived classes.
- You want the safety of having the compiler enforce the identical element type and calling sequence.
You normally use shadowing in the following cases:
- You anticipate that your base class might be modified and define an element using the same name as yours.
- You want the freedom of changing the element type or calling sequence.
suppose you have this classes:
class A {
public int Foo(){ return 5;}
public virtual int Bar(){return 5;}
}
class B : A{
public new int Foo() { return 1;}
public override int Bar() {return 1;}
}
then when you call this:
A clA = new A();
B clB = new B();
Console.WriteLine(clA.Foo()); // output 5
Console.WriteLine(clA.Bar()); // output 5
Console.WriteLine(clB.Foo()); // output 1
Console.WriteLine(clB.Bar()); // output 1
//now let's cast B to an A class
Console.WriteLine(((A)clB).Foo()); // output 5 <<<--
Console.WriteLine(((A)clB).Bar()); // output 1
Suppose you have a base class and you use the base class in all your code instead of the inherited classes, and you use shadow, it will return the values the base class returns instead of following the ineritance tree of the real type of the object.