-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chaining-only constructors #14018
Comments
What about regular expression-bodied methods with no body (e.g. an empty virtual method)? |
Would it be enough to just make this an acceptable style? public LocalDate([NotNull] Era era, int yearOfEra, int month, int day)
: this(era, yearOfEra, month, day, CalendarSystem.Iso) {} |
The proposal is to reduce the syntax to allow one character instead of two for this one infrequent case. That doesn't seem worth the bother to me. |
The benefit wouldn't be so much the single-character reduction (although I note that expression-bodied constructors don't reduce the character count at all...) but the clearer indication of "this constructor just chains to another, it's not intended to do anything else". But I'm certainly not so attached to the idea that I'm going to argue for it vehemently :) |
I've actually thought this is about an access modifier for constructors that only permits usage in the constructor initializer; basically the same statement inverted: the indication of "this constructor is intended to be chained to another and should not be used as a standalone constructor"; On the second thought, it could be implemented via an analyzer. PS: Same idea for void virtual: #8985. |
I've only just found out about expression-bodied everything potentially making it into a later edition of C#, whilst writing about what you can't do with expression-bodied members in the 4th ed of C# in Depth :)
Given the status of that feature, I doubt that this feature request will make it into the same release, but has there been any consideration of constructors with no body, rather than those with a body which is a single statement?
In my experience, I have two kinds of constructor:
In other words, where properties/methods/etc having 1 statement is a sort of natural base case, for constructors it feels like having 0 statements is a natural base case. It would be nice to allow this with just a semi-colon.
For example, from Noda Time's LocalDate class,
would become
Apologies if this has already been covered in a discussion somewhere that I've failed to find. Suggest @gafter comments first, being close to the new feature. (And hey, maybe it's already implemented there and I didn't spot it in the tests...)
The text was updated successfully, but these errors were encountered: