[Proposal] Multiple primary constructors. #9730
Replies: 3 comments 6 replies
-
What advantage does having multiple primary constructors bring over having 2 regular constructors? You can already separate constructors from methods easily enough by just maintaining order in your definitions. The likelihood that adding a new primary constructor would not change the existing code is extremely low, as it would invalidate any constructor parameter captures in the rest of the body. In general, the concept of "multiple primary" is somewhat conflicting. |
Beta Was this translation helpful? Give feedback.
-
The scoping doesn't make sense. How can you have a You can already declare secondary constructors which call the primary constructor, that is the correct way to do this: class Foo(int x, int y) {
Foo(Point pt) : this(pt.X, pt.Y) { }
// x and y are in scope here, pt is not
} |
Beta Was this translation helpful? Give feedback.
-
class Foo
(int x, int y) : BaseFoo(a, b),
(Point point) : BaseFoo(point)
{
// methods ...
} I don't understand the semantics here. How would methods know if they could access x, y, or point? Some 'Foos' would be instantiated with an x and y and no point, and vice versa. No methods would be safe to write. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, in C#, classes can have only one primary constructor.
However, it could be useful to have multiple primary constructors.
Example of syntax/variant 1:
Behave like just multiple regular constructors without ctor-method body.
Equals to code:
Example of syntax/variant 2:
Variables from both constructors and
united
and have default values.Equals to code:
Beta Was this translation helpful? Give feedback.
All reactions