C# Base Constructor

Base keyword

Base is used in constructors. A derived class constructor is required to call the constructor from its base class. When the default constructor isn't present, the custom base constructor can be referenced with the base keyword, directly in the constructor signature. This prevents derived classes from causing a compile-time error.

Keywords

This C# program introduces the base keyword. Base refers to base class.

Example

Note

The program uses a base class, and a derived class, and both of the classes use a parameterful constructor. The derived class must use a base constructor initializer in its constructor declaration. The base constructor initializer is specified by adding a colon and the base keyword after the derived constructor parameter list.

Program that uses base constructor initializer [C#]

using System;

public class A // This is the base class.
{
    public A(int value)
    {
	// Executes some code in the constructor.
	Console.WriteLine("Base constructor A()");
    }
}

public class B : A // This class derives from the previous class.
{
    public B(int value)
	: base(value)
    {
	// The base constructor is called first.
	// ... Then this code is executed.
	Console.WriteLine("Derived constructor B()");
    }
}

class Program
{
    static void Main()
    {
	// Create a new instance of class A, which is the base class.
	// ... Then create an instance of B, which executes the base constructor.
	A a = new A(0);
	B b = new B(1);
    }
}

Output

Base constructor A()
Base constructor A()
Derived constructor B()
This section provides information

Description. This program contains three class declarations: class A and class B both introduce constructors and they are the focus of the demonstration. The class A is the parent or base class for class B, which is referred to as the derived class. The "B : A" syntax indicates that class B derives from class A. Class A and class B both have custom constructors, and the constructor in class B calls into the constructor of class A using the base initializer syntax.

Class Examples

Base constructor syntax. The program shows how you can specify that the base class constructor should be called upon entry to the derived constructor. In the B() constructor, we specify ": base(value)" syntax, which tells the compiler to insert the base class constructor call at the start of the method body.

Note: In programs with non-default constructors, you must specify the base constructor initializer with correct syntax or the program won't compile.

This constructor initializer

This keyword

There is another keyword that can be used in a constructor initializer in the same way as base(). You can use this() with the argument list of another constructor declaration in the same exact type. This does the same thing conceptually as base but for the same class, not the inherited class.

This Constructor Initializer

Summary

The C# programming language

We explored the base constructor initializer syntax in the C# language. You must specify the base initializer when deriving from types with non-default constructors. In the base constructor initializer, you have access to the formal parameter list and can refer those values to the base constructor invocation. The base initializer is similar in syntax and concept as the "this" initializer.

Constructor Tips
.NET