The new
keyword may bring you a better understanding. When you define a method in a class, it is new
by default(if you didn't mark it with abstract/virtual/override/etc), which means this method has no business with those defined in the base class, no matter they have the same name.
class A
{
public virtual void Foo() { }
}
class B : A
{
public new void Foo() { }
public new void Bar() { }
public void Baz() { } //it's also new
}
Now suppose we have B b = new B();
. If you were asked which method would be invoked by b.Bar()
, you will give the correct answer: the method Bar in class B. But you maybe confused if the question is about b.Foo()
, just think it's new in B, don't care those methods in A. And so does b.Baz()
.
EDIT Now here comes A a = new B();
. What will happen if a.Foo()
? Just think the Foo
defined in B has no business with A, since a is defined as A, the method Foo in A is invoked.