Why can’t I access the constructor in the body of another constructor?

I know that I can access the constructor on another constructor, as shown below, but is there a way to access it in the body of the constructor?

public Rectangle(int size) : this(size, size) { Console.WriteLine("Square Constructor Called"); //this(size,size); i want to access like this } 
+4
source share
6 answers

You cannot do this. The full rationale is here , but in the end:

In short, achieving the desired construction management flow is easy to do without adding [the ability to call base constructors in arbitrary places], and there is no attractive benefit to adding this function. No new interesting presentation power is added to the language.

+7
source

Designers can chain only once, basically - which must be specified in front of the body of the designer.

Typically, a solution that requires binding to multiple constructors is to have one ā€œmainā€ constructor, to which all other constructors are ultimately bound, and which does whatever you may need.

+4
source

Good, curious. If you do like this:

 public Rectangle(int w, int h) { Initialize(w, h); } public Rectangle(int size) { Console.WriteLine("blah"); Initialize(size, size); } private void Initialize(...) {...} 
+4
source

No, you cannot access it this way. See more details.

+2
source

I would say, "Unfortunately, no," but it is not available in design. If you cannot or do not want to use this syntax, your only option is to create an additional method that will be populated for the constructor you create.

+1
source

:this(foo, bar) is syntactic sugar, and you cannot do anything with it, which you cannot do in other ways. However :base(foo, bar) not syntactic sugar, as this is the only way to call this constructor when building a derived class, especially considering that you must first have a fully built base class that executes any invariants defined in this class (e.g. if for some of bar only some values ​​of foo valid, then it will throw an exception before moving on).

Method :base(foo, bar) allows classes to use encapsulation to ensure that they never end up in an invalid state, while preserving inheritance, this(foo, bar) just copies the same mechanism as a programmer’s convenience when there is common code used by more than one constructor.

Now, let's say we decide that :this(foo, bar) can be called from anywhere. This is essentially a method call. Well, we can still do this in the class constructor. We have lost syntactic similarities with :base(foo, bar) (and we believe that some people are already familiar with this C ++, which lowered their learning curve) and simply added a threat method to a constructor similar to the void return method, adding the complexity of the check is in the constructor’s body (and one has to deal with people asking why it can’t be called elsewhere, when now it seems that it should be possible, or it’s weirdness to allow it to be done), when people can simply create such methods.

All in all, I think it was a good design decision.

0
source

All Articles