вторник, 20 сентября 2011 г.

C# - new keyword in method signature


New keyword reference from MSDN:
Here is an example I found on the net from a Microsoft MVP that made good sense: Link to Original
public class A{
   public virtual void One();
   public void Two();
}
public class B : A{
   public override void One();
   public new void Two();
}

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

a.One(); // Calls implementation in B
a.Two(); // Calls implementation in A
b.One(); // Calls implementation in B
b.Two(); // Calls implementation in B
Override can only be used in very specific cases. From MSDN:
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.
So the 'new' keyword is needed to allow you to 'override' non-virtual and static methods.

Комментариев нет: