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.
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.