Search code examples
c#constructor

Is it possible to access a constructor in the body of another constructor?


I know that I can access a constructor on another constructor like below:

public Rectangle(int size) : this(size, size)
{
    Console.WriteLine("Square Constructor Called");
    //this(size,size); I want to access like this 
}

Is there a way to access it in the body of another constructor, instead?


Solution

  • You can't do that. The full justification is here, but in summary:

    In short, achieving the desired construction control flow is easy to do without adding the [the ability to call base constructors in arbitrary locations], and there's no compelling benefit to adding the feature. No new interesting representational power is added to the language.