3

Can someone please explain the following constructor syntax to me. I haven't come across it before and noticed it in a colleagues code.

public Service () : this (Service.DoStuff(), DoMoreStuff())
{ }
2
  • 3
    It runs another constructor passing there 2 results from 2 static methods Commented Aug 20, 2012 at 11:26
  • This is called constructor-chaining. Commented Aug 20, 2012 at 11:29

2 Answers 2

7

It chains to another constructor in the same class. Basically any constructor can either chain to another constructor in the same class using : this (...), or to a constructor in the base class using : base(...). If you don't have either, it's equivalent to : base().

The chained constructor is executed after instance variable initializers have been executed, but before the body of the constructor.

See my article on constructor chaining or the MSDN topic on C# constructors for more information.

As an example, consider this code:

using System;

public class BaseClass
{
    public BaseClass(string x, int y)
    {
        Console.WriteLine("Base class constructor");
        Console.WriteLine("x={0}, y={1}", x, y);
    }
}

public class DerivedClass : BaseClass
{
    // Chains to the 1-parameter constructor
    public DerivedClass() : this("Foo")
    {
        Console.WriteLine("Derived class parameterless");
    }

    public DerivedClass(string text) : base(text, text.Length)
    {
        Console.WriteLine("Derived class with parameter");
    }

}

static class Test
{
    static void Main()
    {
        new DerivedClass();
    } 
}

The Main method calls the parameterless constructor in DerivedClass. That chains to the one-parameter constructor in DerivedClass, which then chains to the two-parameter constructor in BaseClass. When that base constructor completes, the one-parameter constructor in DerivedClass continues, then when that finishes, the original parameterless constructor continues. So the output is:

Base class constructor
x=Foo, y=3
Derived class with parameter
Derived class parameterless
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. So does that syntax mean that this constructor chains to the default constructor for this class and effectively extends it? Thanks for the links. Wil check them out at lunch.
@CSharpened: No, this constructor replaces a default constructor. It chains to a constructor with two parameters. See my example.
Ok thanks again. I will read into it now that I know the term for it and have the articles etc.
6

In this case, there must a a second constructor which will accept two parameters - the return values of Service.DoStuff() and DoMoreStuff(). These two methods must be static methods.

2 Comments

Just for a little clarification before I read up does the syntax effectively run the call to the base class constructor (or first link in the chain) first and then follow the chain back up and call each constructor in turn until the chain ends?
It will call the second constructor on your class first, and then see if this second constructor has a base call - if not, it will call base().

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.