SoFunction
Updated on 2025-03-06

Analysis of the usage of base class constructors in C#

This article describes the usage of the base class constructor in C# when calling derived classes. Share it for your reference. The specific analysis is as follows:

The default constructor here refers to the system's default parameterless constructor without writing the constructor.

1. When the base class does not write the constructor itself, the derived class defaults to call the base class's default constructor.
For example:

public class MyBaseClass
{
}
public class MyDerivedClass : MyBaseClass
{
  public MyDerivedClass()
  {
   ("I'm a subclass parameterless constructor");
  }
  public MyDerivedClass(int i)
  {
   ("I'm a constructor with a parameter in the subclass");
  }
  public MyDerivedClass(int i, int j)
  {
   ("I'm a constructor with two parameters in the subclass");
  }
}

When instantiating the derived class, the default constructor of the base class is called

2. When writing constructors in the base class, when the derived class does not specify which constructor to construct, it will look for constructors without parameters. If not, an error will be reported. In addition, no matter which constructor in the derived class is called, it will look for the base class constructor without parameters, rather than parameter matching.
For example:

public class MyBaseClass
{
  public MyBaseClass(int i)
  {
   ("I'm a constructor with a parameter in the base class");
  }
 }
 public class MyDerivedClass : MyBaseClass
 {
  public MyDerivedClass()
  {
   ("I'm a subclass parameterless constructor");
  }
  public MyDerivedClass(int i)
  {
   ("I'm a constructor with a parameter in the subclass");
  }
  public MyDerivedClass(int i, int j)
  {
   ("I'm a constructor with two parameters in the subclass");
  }
}

At this time, an error is reported when instantiating the derived class

3. A constructor is written in the base class, so the derived class can specify a constructor called the base class and use the base keyword.
For example

 public class MyBaseClass
 {
  public MyBaseClass(int i)
  {
   ("I'm a constructor with a parameter in the base class");
  }
 }
 public class MyDerivedClass : MyBaseClass
 {
  public MyDerivedClass() : base(i)
  {
   ("I'm a subclass parameterless constructor");
  }
  public MyDerivedClass(int i) : base(i)
  {
   ("I'm a constructor with a parameter in the subclass");
  }
  public MyDerivedClass(int i, int j) : base(i)
  {
   ("I'm a constructor with two parameters in the subclass");
  }
 }

When instantiating the constructor with one parameter used when instantiating the derived class, there will be no error because it specifies the constructor of the base class.

4. If the constructor in the base class does not contain a parameterless constructor, then the constructor in the derived class must specify all the base class constructors to be called, otherwise an error occurs.
For example

 public class MyBaseClass
 {
  public MyBaseClass(int i)
  {
   ("I'm a constructor with a parameter in the base class");
  }
 }
 public class MyDerivedClass : MyBaseClass
 {
  public MyDerivedClass()
  {
   ("I'm a subclass parameterless constructor");
  }
  public MyDerivedClass(int i) : base(i)
  {
   ("I'm a constructor with a parameter in the subclass");
  }
  public MyDerivedClass(int i, int j)
  {
   ("I'm a constructor with two parameters in the subclass");
  }
}

Compilation will not be passed at this time

I hope this article will be helpful to everyone's C# programming.