How to Call Base Constructor in C# - Simple Guide
In C#, you call a base class constructor from a derived class using the
base keyword followed by parentheses with any required arguments. This call is placed in the derived class constructor's signature after a colon, like Derived(args) : base(baseArgs).Syntax
The syntax to call a base class constructor in C# is:
DerivedConstructor(parameters) : base(baseParameters) { }Here:
- DerivedConstructor is the constructor of the derived class.
- base calls the constructor of the base class.
- baseParameters are the arguments passed to the base constructor.
csharp
public class BaseClass { public BaseClass(int number) { // Base constructor logic } } public class DerivedClass : BaseClass { public DerivedClass(int number) : base(number) { // Derived constructor logic } }
Example
This example shows a base class with a constructor that takes a string, and a derived class that calls this base constructor using base. It prints messages to demonstrate the order of constructor calls.
csharp
using System; public class Animal { public Animal(string name) { Console.WriteLine($"Animal constructor called with name: {name}"); } } public class Dog : Animal { public Dog(string name) : base(name) { Console.WriteLine("Dog constructor called"); } } class Program { static void Main() { Dog myDog = new Dog("Buddy"); } }
Output
Animal constructor called with name: Buddy
Dog constructor called
Common Pitfalls
Common mistakes when calling base constructors include:
- Forgetting to call the base constructor explicitly when the base class has no parameterless constructor, causing a compile error.
- Passing incorrect or missing arguments to the base constructor.
- Trying to call the base constructor inside the derived constructor body instead of using the
basekeyword in the constructor signature.
Always ensure the base constructor call matches the base class constructor signature.
csharp
public class BaseClass { public BaseClass(int number) { } } public class DerivedClass : BaseClass { // Wrong: no base call, will cause error if BaseClass has no parameterless constructor // public DerivedClass(int number) { } // Correct: public DerivedClass(int number) : base(number) { } }
Quick Reference
Remember these tips when calling base constructors:
- Use
base(arguments)after the derived constructor signature. - The base constructor call must be the first thing in the constructor signature.
- If the base class has no parameterless constructor, you must call one explicitly.
- You cannot call the base constructor inside the constructor body.
Key Takeaways
Use the base keyword in the derived constructor signature to call the base constructor.
The base constructor call must come after a colon (:) following the derived constructor parameters.
If the base class has no parameterless constructor, calling base explicitly is required.
Do not call the base constructor inside the constructor body; it must be in the signature.
Match the base constructor parameters exactly to avoid compile errors.