New

A new modifier can be used to replace a method in a base class.
The method defined in the derived class is completely independent of the method with the same name in the base class.

public class Base 
{
   public void F() {}
}

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

This is required when the method in the base class has not been declared as virtual.


Hidden members can still be called if an instance of the derived class is cast to an instance of the base class ??

Derived B = new Derived(); 
B.Method(); //calls the new method

Base B = new Derived();
A.Method(); //calls the old method

Cannot be used with override


© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext