constructor base call without empty curly brackets #4309
Replies: 3 comments 29 replies
-
To help the parser, this would only be done by completing the constructor declaration with a semicolon, like so: public TestB(TestA a) : base(a); And does this really save much over public TestB(TestA a) : base(a) { } Additionally, ending a method declaration in a semicolon has a clear meaning in C#. It is done for abstract, partial or extern methods, all of which do have a body, it is just not specified at the point where the method is declared. |
Beta Was this translation helpful? Give feedback.
-
While one could argue that records and primary constructors give precedence to empty method bodies (from their ability to have empty type definitions), I, personally, do not like the idea of empty methods being denoted with a single semicolon. It could also conflict with default interface methods (DIMs) where a |
Beta Was this translation helpful? Give feedback.
-
This proposal is useful for classes that use a Primary Constructor on C#12. public class Sample(int id, string name)
{
public Sample(): this(0, "");
} The difference between // Editor might be written:
public Sample(): this(0, "") {}
// With whitespace...?
public Sample(): this(0, "") { }
// Or line break...?
public Sample(): this(0, "")
{
} |
Beta Was this translation helpful? Give feedback.
-
When writing C# constructors with a base call the following code is often created:
The main thing about this is that sometimes the curly brackets in TestB do not contain any code. it would be nice to be able to just remove those. Like in this example:
An alternative I currently see used a lot in this case is the following but it just adds a bit of clutter to the end of the line in my opinion:
If you have any questions (or if it isn't clear) please do not hesitate to ask.
Beta Was this translation helpful? Give feedback.
All reactions