26

Title may sound confusing. What I want is to call a constructor of the same class and the constructor of the base class inside a constructor. Maybe my first attempt to solve that may explain my question:

public MyClass(MyClass obj) : base(obj),this() {}

But that notation isn't working. Is there any solution to solve that?

1
  • 2
    What if your parameterless constructor (this()) specified a different base class constructor (which it implicitly does - the parameterless one - if not specified). Which base class constructor should be invoked? Commented Jun 16, 2011 at 7:20

7 Answers 7

21

No, you can't do it for the following reason:

When a constructor calls the constructor of its base, the latter call is THE PART of the constructor in question. So you can't call another constructor of the same class AND a constructor of a base class because the former call already contains a call to a base constructor - you can't initialize your base twice

Sign up to request clarification or add additional context in comments.

Comments

8

C# allows either base(...) or this(...), because every constructor must eventually invoke a super constructor. So if it were possible to invoke both base() and this(), two super constructors invocations would take place, which would be fundamentally incorrect.

It is the same reason why it is not possible to invoke base(...) twice.

2 Comments

Ok, I understand why it is not possible to solve it like I tried. So it seems that I have to use a workaround.
Please read carefully all other people's answers on an alternative
2

No technical solution for this, you need a workaround, move logic out of the default constructor, make the base call a virtual function than override at your super class.

3 Comments

Calling virtual methods in a constructor is generally not good style.
Do you mean "make the base-class call something", or "make the base-call something"? Also, why should one override a virtual function inside the base-class? Virtual functions are to be overridden in subclasses.
Calling a virtual method from isn't a good way to solve this. See: blogs.msdn.com/b/ericlippert/archive/2008/02/18/… And Eric's comment on 18 Feb 2008 6:50 PM
2

One way would be to make the parameterless constructor of MyClass to call base(obj). But if you don't always want to do that, than I don't think there is a good way.

Maybe you can try to move stuff around in those constructors. Can you post more info about what actually happens in these constructors?

2 Comments

It is for implementing copy constructors. So that I can call the "default stuff" of the parameterless constructor and can call the base constructor.
can't you put the "default stuff" of the parameterless constructor in the base? Or just copy it to the other constructor?
1

You could simply copy the constructor code into the given constructor. Or make a special setup() function that is called from the default constructor and this constructor.

1 Comment

Thanks for your post. Yes, this is a solution I considered. But I hope that there maybe a solution like calling base() and this() in a constructor, like it is possible in e.g. Java.
0

In C# you may also provide a default initialization in the declaration for each member:

private int foo_ = 41;

This solves for many use-other-constructor issues.

Comments

-2

Actually, you CAN do so, You can fire the parent class constructor from the child class instance, here is how you do it:

public class Person
{
    public Person(int age) {
        this.Age = age; 
    } 
    public int Age;
}

public class students :Person
{
    public students(string name, int age) :base(age ) 
    { 
        this.studentName = name;
        this.Age = age;
    }

    public string studentName;
}

Now, lets say you are creating an object from students class, So, you will do this:

students std = new students ("Jack", 23);

This will send "Jack" to students class constructor, and 23 will be send to Person constructor via :base(age)

Hope this help, Cheers.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.