C#:NET:Class

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, with base, be referenced.

Note:In a class, we can also access fields and other members (like methods) with the "base" and "this" keywords.


Example. The program uses a base class and a derived class. Both of the classes use a non-default, parameterful constructor. The derived class must use a base constructor initializer, with the base keyword, in its constructor declaration.

Tip:This initializer is specified by adding a colon and the base keyword after the derived constructor parameter list.

Based on: .NET 4.5

C# program that uses base constructor initializer

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.
	// ... B executes the base constructor.
	A a = new A(0);
	B b = new B(1);
    }
}

Output

Base constructor A()
Base constructor A()
Derived constructor B()
In this program, class A and class B both introduce constructors. 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.

Explanation:In the example, the constructor in class B calls into the constructor of class A using base initializer syntax.

Class
We specify that the base class constructor is called upon entry to the derived constructor. In the B constructor, we use base initializer syntax. The compiler inserts the constructor call at the start of the method body.

Note:For non-default constructors, you must specify the base constructor initializer with valid arguments.


This initializer. 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.

Tip:This does the same thing conceptually as base but for the same class, not the parent class.

Constructor Initializer
Base vs. This. Let us compare base and this. In a derived class, the base and this keywords can be used to reference members. These keywords disambiguate members. They eliminate confusion as to which member we want.

Derived:When we have a derived class, we can use a "base" expression to directly access the base class.

Output:The program accesses first the base _value, which equals 6. And then it gets the this _value, which is 7.

Disambiguate:This is a fancy word that means "to make clear" which entity you are referring to.

C# program that uses base and this keywords

using System;

class Net
{
    public int _value = 6;
}

class Perl : Net
{
    public new int _value = 7;
    public void Write()
    {
	// Show difference between base and this.
	Console.WriteLine(base._value);
	Console.WriteLine(this._value);
    }
}

class Program
{
    static void Main()
    {
	Perl perl = new Perl();
	perl.Write();
    }
}

Output

6
7
We use these keywords to resolve ambiguous expressions in class definitions. If the base and this keywords were removed, the compiler would not know the difference between _value fields.

Constructors:The "base" and "this" keywords are also used in constructor initializers. These make constructors easier to write.

Thus:Base and this are needed for navigating the class hierarchy. With them, we access members from a targeted class.

Inheritance
Summary. The base initializer is similar in syntax and concept as the this-initializer in constructors. We specify the base initializer when deriving from types with non-default constructors. In this initializer, we can access all parameters.