Simplified Class Definitions #4844
-
This is a duplicate of #4722 . I had missed it in the discussions list before I drafted this. With the changes proposed for C#10, we get File-Scoped Namespaces. That allows the following simplification to be made to code: namespace Program {
class Thing {
...
}
} to namespace Program;
class Thing {
...
} The change saves both source code size and horizontal space from removing the indentation. However, I was thinking that it would also make sense to bring this to I'm a fan of creating classes in another class file and then moving it with Quick Actions and Refactoring. This leads to projects that primarily have one class per file. These files typically look as so: using ...;
namespace Program {
class Thing {
void A() {
...
}
and so on
}
} Combined with both File-Scoped Namespaces and the new Simplified Class Definitions, the resulting code would look as such: using ...;
namespace Program;
class Thing;
void A() {
...
}
and so on It looks similar to code like C#9's Top-Level Statements. Considering that this is only a simplification, and that its syntax is currently invalid, I don't believe this should create any issues with backwards compatibility. SyntaxIt should also be possible to use all existing modifiers, e.g. ImplementationUnder the hood, it could be very simple to implement the new syntax, perhaps even as simple as replacing the If there were more than one SCD, we'd simply move the ConsiderationsIt can look very messy if there are multiple classes defined in the same file with the SCD syntax. Since everything is on the first indentation, it creates a difficult-to-read mess where finding the class boundaries would be difficult. However, I think this is a similar problem to writing code without pressing the ENTER key. Put differently, it's a bad idea and nobody would really want to do it, but it is possible. Please let me know if you think of something that I've missed or haven't considered well enough. I'm not a language designer, I'm just a developer who loves C# and watching it evolve. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I had searched under a different set of keywords when I was working on drafting this discussion. Searching again, I found #4722 which already closes this. |
Beta Was this translation helpful? Give feedback.
I had searched under a different set of keywords when I was working on drafting this discussion. Searching again, I found #4722 which already closes this.