Pages

Tuesday, 9 April 2013

C# Base and Constructor Initializer: How to overload constructors in C#?

C# Base and Constructor Initializer: How to overload constructors in C#?

Base is used in constructors. A derived class constructor is required to call the constructor from its base class.

Example

The program uses a base class and a derived class. The derived class must use a base constructor initializer, with the base keyword, in its constructor declaration. This initializer is specified by adding a colon and the base keyword after the derived constructor parameter list.
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()

Explanation

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. The constructor in class B calls into the constructor of class A using base initializer syntax.

C# Constructor Initializer

This is used in constructors. It is used to overload the constructors in a C# program. It allows code to be shared between the constructors. Constructor initializers, which use the this-keyword, prove useful in nontrivial classes.

Example

This program contains a class called Mouse with two public constructors. The first constructor is parameterless and it calls into the second constructor, using this-constructor initializer syntax.

The this-keyword in this context instructs the compiler to insert a call to the specified constructor at the top of the first constructor. The target constructor is determined through the process of overload resolution.

using System;
class Mouse
{
    public Mouse()
                              : this(-1, "")
    {
           // Uses constructor initializer.
    }
   
    public Mouse(int weight, string name)
    {
        // Constructor implementation.
        Console.WriteLine("Constructor weight = {0}, name = {1}", weight,  name);
    }
}

class Program
{
    static void Main()
    {
       // Test the two constructors for Mouse type.
       Mouse mouse1 = new Mouse();
       Mouse mouse2 = new Mouse(10, "Sam");
    }
}

Output
Constructor weight = -1, name =
Constructor weight = 10, name = Sam

Explanation:

We see the this-keyword in the context of a constructor. To use the this-keyword, specify it after a colon following the parameter list. The constructor name must match that of the declared class where you are adding it.

No comments:

Post a Comment