Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling base constructor in C# [duplicate]

Tags:

c#

asp.net

People also ask

How do you call the base class constructor?

How to call the parameterized constructor of base class in derived class constructor? To call the parameterized constructor of base class when derived class's parameterized constructor is called, you have to explicitly specify the base class's parameterized constructor in derived class as shown in below program: C++

Do you have to call base class constructor?

No. You never need to explicitly call a destructor (except with placement new ). A derived class's destructor (whether or not you explicitly define one) automagically invokes the destructors for base class subobjects. Base classes are destructed after member objects.

What is base in constructor?

A base class access is permitted only in a constructor, an instance method, or an instance property accessor. It is an error to use the base keyword from within a static method. The base class that is accessed is the base class specified in the class declaration.

Is Base constructor called First C#?

In C# terms, the base constructor is executed first.


You have to call the base class constructor prior to the derived class constructor's body.

class Derived : Base
{
  public Derived(string someParams)
    : base("Blah " + someParams)
  {

  }

}

You can't. You can call it before:

public Derived() : base()

or you have to use a hook

class Base
{
  protected void init() { }
  public Base(string sMessage)
  {
     init();
  }
}

class Derived : Base
{
  public Derived(string someParams)
  {
   string sMessage = "Blah " + someParams;
   init();
  }
}

I originally missed OregonGhost's comment about using a static method to modify the parameter, which turned out to be the most useful for me, so I thought I'd add a code sample for others who read this thread:

class Base
{
    public Base( string sMessage )
    {
        // Do stuff
    }
}

class Derived : Base
{
    public Derived( string sMessage ) : base( AdjustParams( sMessage ) )
    {
    }

    static string AdjustParams( string sMessage )
    {
        return "Blah " + sMessage;
    }
}

If you really need to have your constructor run first, then I suggest using a protected Initialize method that is invoked by your constructors and does the actual work of initializing the class. You need to provide an alternate constructor that will allow the initialization to be skipped.

public class Base
{

    public Base() : this(true) { }

    protected Base(bool runInitializer)
    {
        if (runInitializer)
        {
            this.Initialize();
        }
    }

    protected void Initialize()
    {
        ...initialize...
    }
}

public class Derived : Base
{
    // explicitly referencing the base constructor keeps
    // the default one from being invoked.
    public Derived() : base(false)
    {
       ...derived code
       this.Initialize();
    }
}

Points to be noted on constructors:

· Constructors cannot be "virtual".

· They cannot be inherited.

· Constructors are called in the order of inheritance.

public Child(string a):base(a){}