asakura89
asakura89

Reputation: 531

calling base method using new keyword

in this link, they have this code:

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

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

and then called like this:

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

my actual code is this:

public class Base
{
   public void Method()
   {
        // bla bla bla
   }
}

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

is it necessary to call with base.Method(); ?
or just leave the method in derived class blank ?

Upvotes: 10

Views: 6023

Answers (1)

ABCD
ABCD

Reputation: 907

you need 'base' if you really need to call the base class's method. base.Method(); is the correct way.

Knowing When to Use Override and New Keywords (C# Programming Guide)

Upvotes: 7

Related Questions