Why can't access constructor in body of another constructor?
I know that i can access constructor on another constructor like below but is there a 开发者_Go百科way to access it in body of constructor ?
public Rectangle(int size) : this(size, size)
{
Console.WriteLine("Square Constructor Called");
//this(size,size); i want to access like this
}
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.
Constructors can only chain once, basically - which has to be specified before the body of the constructor itself.
Normally the solution to wanting to chain to multiple constructors is to have one "master" constructor which all other constructors chain to eventually, and which does everything you could want.
Well, kinda. If you make it look like this:
public Rectangle(int w, int h) {
Initialize(w, h);
}
public Rectangle(int size) {
Console.WriteLine("blah");
Initialize(size, size);
}
private void Initialize(...) {...}
No, you cannot access it that way. See this for further details.
I would say "Unfortunatally not" but it's not available by design. If you can't or don't want to use the given syntax, your only other option is to create an additional method that would full in for the constructor you seek.
:this(foo, bar)
is syntactic sugar, there's nothing you can do with it that you can't do other ways. However, :base(foo, bar)
is not syntactic sugar, as it is the only way to call the constructor in question during construction of the derived class, especially considering that you must have a fully-constructed base class first, which fulfills any invariants defined in that class (e.g. if only some values of foo
are valid for some of bar
then it will throw an exception before you go further).
The way :base(foo, bar)
works enables classes to use encapsulation to ensure that they are never put into an invalid state, while still allowing for inheritance, this(foo, bar)
simply copies the same mechanism as a convenience for the programmer when there is common code used by more than one constructor.
Now, let's say we decided to have :this(foo, bar)
callable from anywhere. It would essentially be a method call. Well, we can already do that in a class constructor anyway. We've lost the syntactic similarity to :base(foo, bar)
(and consider that some people are already familiar with that C++, which lowered their learning-curve), and just added a a way to threat a constructor like a void-returning method, adding the complexity of checking it is in the body of a constructor (and having to deal with people asking why it can't be called elsewhere when it now looks like it should be possible, or else the weirdness of letting it be done), when people could just create such methods anyway.
In all, I think it was a good design decision.
精彩评论