Ant's
Ant's

Reputation: 13811

What actually Base Keyword does in Constructor initialization?

I have been confused by the base keyword in C#. Say I have some code like this:

​class A
{
    ​// member declaration....
    A(int s, int t, int x)
    {
        ​// assign the values of members
    }
}

class B : A
{
    // member declaration....
    ​B(int a, int b, int c, int d) : base(a,b,c)
    {
        // does the base keyword play a role in
        // this constructor?
    }
}
​

Now what is the exact use of base keyword in B's constructor? I have few questions to clarify:

  1. Using the base keyword will call the base class A constructor(in our example)? Is this correct?
  2. Providing the base keyword in derived class changes the behavior of the derived class constructor?

And basically when base keyword should be used in the constructor(some examples would be good)?

Edit:

I have another question. How can I inform the base class about our derived class, via base keyword?

Thanks in advance.

Upvotes: 8

Views: 11066

Answers (3)

CodeCaster
CodeCaster

Reputation: 151584

I think the manual is pretty clear on base:

The base keyword is used to access members of the base class from within a derived class:

  • Call a method on the base class that has been overridden by another method.

  • Specify which base-class constructor should be called when creating instances of the derived class.

public class BaseClass
{
    int num;

    public BaseClass()
    {
        Console.WriteLine("in BaseClass()");
    }

    public BaseClass(int i)
    {
        num = i;
        Console.WriteLine("in BaseClass(int i)");
    }

    public int GetNum()
    {
        return num;
    }
}

public class DerivedClass : BaseClass
{
    // This constructor will call BaseClass.BaseClass()
    public DerivedClass() : base()
    {
    }

    // This constructor will call BaseClass.BaseClass(int i)
    public DerivedClass(int i) : base(i)
    {
    }

    static void Main()
    {
        DerivedClass md = new DerivedClass();
        DerivedClass md1 = new DerivedClass(1);
    }
}

/*
Output:
in BaseClass()
in BaseClass(int i)
*/

Upvotes: 7

Dan Bryant
Dan Bryant

Reputation: 27495

  1. Yes, this calls the base class constructor.

  2. A derived constructor always calls a base class constructor; if you don't specify one, it will try to call the parameterless constructor (if one exists.) This syntax lets you pass parameters to a specific base class constructor. Note that, in this case, there is no public or protected base class constructor without parameters, so you need to use the 'base' syntax with parameters.

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1500385

Yes - base chains to a constructor in the base class.

If you don't specify base or this (to chain to another constructor in the same class), the effect is as if you had base() chaining to a parameterless constructor in the base class. In your example this would cause a compilation failure as your base class doesn't have a parameterless constructor.

See my article on constructors for more information.

Upvotes: 6

Related Questions