Can someone give me 1 good reason why in C# the chained constructor is always called before any of the constructor body?
.NET allows you to call the chained constructor at any point within the constructor, so why does C# force you to do it before your constructor body executes?
I once wrote to Anders H and asked him this and he was kind enough to spend the time replying despite how busy he must be. Unfortunately he managed to answer a question I didn’t actually ask (about named constructors.)
So, just out of curiosity I thought I would ask here because personally I don’t think there is a single good reason for this limitation, so hopefully I will be reeducated 🙂
Just to clarify. The .NET CLR rule is that 1 constructor must be called, only 1 constructor, and only once. So in the CLR these are valid
public class Meh { public Meh() { Console.WriteLine('Meh()'); this('Hello'); } public Meh(string message) { Console.WriteLine('Meh {0}', message); base(); } }
But not in C#
If there were a genuine reason why this ability should not be provided then it wouldn’t be supported by the CLR or other .NET languages. I can only conclude that the answer is one of those ‘Because it’s always been this way’ ones, and the restriction was probably just copied from C++ or something.