Calling a base class' method

C#Oop

C# Problem Overview


In c++ I would do

class A
{
public:
    virtual void stuff()
    {
        //something
    }
};

class B : public A
public:
    virtual void stuff()
    {
        //something2
        A::stuff() //something
    }
};

How would I do this in C#? I've tried

public void stuff()
{
    //something2
    A.stuff(); //something
}

but that doesn't work

C# Solutions


Solution 1 - C#

base is the keyword for referencing your superclass in C#. Use:

base.stuff();

Solution 2 - C#

Use base. Like base.stuff();

Solution 3 - C#

Just to add to the answer above, base.stuff() works, unless it's the constructor you're trying to call in which case it is called as:

class A
{
public:
    public A(){}

};

class B : A
{
    public B() : base()
    {
   
    }
};

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAvery3RView Question on Stackoverflow
Solution 1 - C#user541686View Answer on Stackoverflow
Solution 2 - C#Alex AzaView Answer on Stackoverflow
Solution 3 - C#DllewellynView Answer on Stackoverflow