0

I would like to override a method, but i still want the parent method to be called when i override. When i try to use :base() to call parent method, it says unexpected token.

public class A 
{
    public virtual void DoStuff() 
    {
       //some code
    }
}

public class B : A 
{
    public override void DoStuff() : base()
    {
       //some other code
    }
}

In java i would be :

super.DoStuff()
macTAR
  • 83
  • 12

1 Answers1

3

You have the wrong syntax, you can call the base method using the base keyword anywhere in the body of the method. The :base() syntax is only for constructors.

public class A 
{
    public virtual void DoStuff() 
    {
       //some code
    }
}

public class B : A 
{
    public override void DoStuff()
    {
         base.DoStuff();
    }
}
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357