new keyword in method signature

New keyword reference from MSDN:

MSDN Reference

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.


No, it's actually not "new" (pardon the pun). It's basically used for "hiding" a method. IE:

public class Base
{
   public virtual void Method(){}
}

public class Derived : Base
{
   public new void Method(){}
}

If you then do this:

Base b = new Derived();
b.Method();

The method in the Base is the one that will be called, NOT the one in the derived.

Some more info: http://www.akadia.com/services/dotnet_polymorphism.html

Re your edit: In the example that I gave, if you were to "override" instead of using "new" then when you call b.Method(); the Derived class's Method would be called because of Polymorphism.