: this(foo) syntax in C# constructors?
Asked Answered
P

7

17

Every now and then, I bump into syntax that I've seen before, but never used. This is one of those times.

Can someone explain the purpose of ":this" or ":base" following a C# constructor method?

For example:

public MyClass(SomeArg arg) : this(new SomethingElse(), arg)
{
}

My gut feeling is that it is used to map a default argument onto another constructor method.

Placement answered 3/12, 2008 at 19:16 Comment(0)
P
20

You're basically right. this() calls a constructor on the current instance, base() calls the supertype's constructor on current instance. They're generally used to handle constructor overloads so you can add additional options without breaking things out into a separate method.

Phospholipide answered 3/12, 2008 at 19:19 Comment(0)
H
20

Your gut feeling is right. The syntax is used to call overloaded constructors in the same class:

public class Test
{
    public Test() : this("Called from default constructor") { }
    public Test(String msg)
    {
        Console.WriteLine(msg);
    }
}

The following code:

public static void Main(String[] args)
{
    Test t1 = new Test();
    Test t2 = new Test("Called from Main function");
}

Outputs the following

Called from default constructor
Called from main function

Similarly, : base(someParams) is used to call base constructors.

Haugh answered 3/12, 2008 at 19:21 Comment(0)
G
4

You're right.

: base(...) calls a constructor of the base class.

: this(...) calls another constructor of the defining class. Most of the time it works merely as a facade.

Gorey answered 3/12, 2008 at 19:20 Comment(0)
S
3

Every constructor in a .NET class ensures that a constructor in the class it inherits from is also called.

So if you have the following classes:

public class Base { }
public class Something : Base { }
public class Else : Something { }

then a constructor in Else, will call a constructor in Something, which will also call a constructor in Base.

The constructor called in a base class (ie. the one you're descending from) is always the parameterless constructor.

If you don't have one, or want to override that, you can override it, by specifying base(some parameters here). This will pick the right constructor in the base class.

You can also ask a constructor to first call another constructor in the same class, at the same level. This can be used to avoid duplicating constructor code in multiple constructors. Ultimately though, the constructors being called will call a constructor in the base class.

I hope this was understandable.

Superable answered 3/12, 2008 at 19:21 Comment(0)
B
2

Exactly. The call it Constructor Chaining and it's used to get around the fact that C# doesn't have the ability to do default arguments.

This is used a lot in IoC.

Brume answered 3/12, 2008 at 19:22 Comment(0)
B
2

It's like this if i'm not mistaken:

public MyClass(SomeArg arg) : this(new SomethingElse(), arg)

will call

public MyClass(SomethingElse arg, SomeArg arg1) : base or this or nothing

and that will go on until you got a base or nothing.

If you have base(....) then that constructor having that will call the base constructor with the parameters (if any) given, which in turn can delegate to its own constructors (same game).

If you have nothing, then the parameter-less constructor of the base-class is called automatically.

After you have used this(....), then the constructor matching the parameters will be used and its body will be executed - additionally to the body of the constructor having used this(....).

Baran answered 3/12, 2008 at 19:25 Comment(0)
F
2

Yes you are right. This syntax is used to have your subclass constructors to explicitly call an appropriate custom base class constructor, rather than the default. The this keyword in your question is explained:

Another use of the this keyword is to force one constructor to call another in order to avoid redundant member initialization logic.

in Pro C# 2005 and the .NET 2.0 Platform, 3rd Edition by Andrew Troelsen

Foremost answered 3/12, 2008 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.