2

I did some Google search but could not find anything. So I implemented something working with an intermediate constructor.

However, I am wondering whether it is possible to chain more than one constructor at the same time (base + this).

This is my current (simplified) code:

public BlaNode(Node Previous, Node Next) : base(Previous, Previous)
{
    this.Blas = new HashSet<BlaDiBla>();
}
public BlaNode()
    : this(null, null)
{

}
public BlaNode(Node Previous, Node Next, string Bla)
    : this(Previous, Previous)
{

}
2
  • 1
    Is this not working for you already? Commented Aug 2, 2012 at 13:09
  • yes it is but I may not need the intermediate constructor. Commented Aug 2, 2012 at 13:10

3 Answers 3

1

No, you can only call one constructor at a time in a C# constructor initializer. The documentation refers to only one other constructor to call, too:

All instance constructors (...) include an invocation of another instance constructor (...). The constructor to implicitly invoke is determined by the constructor-initializer:

You can make your intermediate constructor private if you do not want any other code to call it.

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

Comments

1

Whether it is possible to chain more than one constructor at the same time (base + this)

No. You can chain either sideways (this), and do so multiple times, or 'up' (base), exactly once.

Note that the notation is closely linked to the order in which the constructor-bodies will be executed.

Your BlaNode(Node, Node) is the one that 'picks' the base constructor. Suppose that BlaNode() was allowed to select a different base constructor. That would only lead to confusion about which ctor(s) will be used, and with what parameters.

So there would be no gain that I can see, and there definitely would be drawbacks.

6 Comments

How can you chain sideways multiple times? I think that statement is incorrect as for every constructor, you can only indicate one chained constructor, be it sideways or upwards.
One time per ctor of course. But the chained ctor can chain again, as in the OPs question.
So can the ctor chained upward.
But you can only execute one : base() per class. And multiple : this()
Sorry, that's wrong. You can execute as many base() in the constructors of one single class as you like. Sometimes, that's even kind-of a pattern: When deriving from Exception, it's often the most reasonable way to declare four constructors with the four default signatures as found in the base class, and have each of them call the respective inherited constructor with the same signature.
|
1

As far as I know this is not possible. You can use static factory methods for this. http://en.wikipedia.org/wiki/Factory_method_pattern

private BlaNode(Node Previous, Node Next) : base(Previous, Previous)
{
    //whatever you do
}

public static BlaNode BlaNodeFactory()
{
    return new BlaNode(null, null);
}

public static BlaNode BlaNodeFactory(Node Previous, Node Next, string Bla)
{
    return new BlaNode(Previous, Previous);
}

1 Comment

They are not quite equivalent, though, as they cannot assign values to readonly fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.