Are you talking about
virtual
-
override
pair versus hiding with
new
?
class Base
{
public void A() { Console.WriteLine("Base.A"); }
public virtual void B() { Console.WriteLine("Base.B"); }
public virtual void C() { Console.WriteLine("Base.C"); }
public void D() { Console.WriteLine("Base.D"); }
}
class Derived : Base
{
public new void A() { Console.WriteLine("Derived.A"); }
public override void B() { Console.WriteLine("Derived.B"); }
public void C() { Console.WriteLine("Derived.C"); }
public void D() { Console.WriteLine("Derived.D"); }
}
static void Main(string[] args)
{
Base inst1 = new Base();
Base inst2 = new Derived();
Derived inst3 = new Derived();
inst1.A();
inst1.B();
inst1.C();
inst1.D();
inst2.A();
inst2.B();
inst2.C();
inst2.D();
inst3.A();
inst3.B();
inst3.C();
inst3.D();
}
Intended use:
1)
virtual
in the base class plus
override
in the derived class is the intended use.
2) non-virtual plus no re-definition in the derived class is the intended use too.
Assumed error (thus, a warning) since it hides the base class method definition:
3)
virtual
without derived
override
is assumed wrong usage.
4) non-virtual
with re-definition in the derived class is assumed wrong usage.
With the keyword
new
you document this the intended use of 3) or 4) above.
I don't know when this usage of
new
is advised.
Cheers
Andi