To call a base constructor in C#, you can use the base() keyword. The
base() keyword takes the parameters of the base constructor as a parameter.
For example, the following code calls the BaseClass() constructor from the
DerivedClass() constructor:
C#
class BaseClass
{
public BaseClass()
{
// This is the base class constructor.
}
}
class DerivedClass : BaseClass
{
public DerivedClass()
{
// This constructor calls the base class constructor.
base();
// This constructor also does other things.
}
}
When the DerivedClass() constructor is called, the base() keyword calls the
BaseClass() constructor first. Then, the DerivedClass() constructor does other things.
Here is an explanation of the code:
The base() keyword is used to call the BaseClass() constructor.
The BaseClass() constructor is the base class constructor for the
DerivedClass class.
The DerivedClass() constructor takes no parameters.
The DerivedClass() constructor calls the base() keyword to call the
BaseClass() constructor.
The DerivedClass() constructor also does other things.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
To call a base constructor in C#, you can use the
base()keyword. Thebase()keyword takes the parameters of the base constructor as a parameter.For example, the following code calls the
BaseClass()constructor from theDerivedClass()constructor:C#
When the
DerivedClass()constructor is called, thebase()keyword calls theBaseClass()constructor first. Then, theDerivedClass()constructor does other things.Here is an explanation of the code:
base()keyword is used to call theBaseClass()constructor.BaseClass()constructor is the base class constructor for theDerivedClassclass.DerivedClass()constructor takes no parameters.DerivedClass()constructor calls thebase()keyword to call theBaseClass()constructor.DerivedClass()constructor also does other things.