Multiple Access Specifier Blocks #8882
-
I always wanted C++ style Multiple Access Specifier Blocks for C#, it's probably one of the biggest features I miss in C# and I'm really disappointed it's not already implemented. This proposal is to introduce Multiple Access Specifier Blocks in C# to allow grouping class or struct members under a single access modifier, similar to the existing feature in C++. This change aims to improve code organization, readability, and maintainability, especially for classes with multiple members sharing the same access level. MotivationCurrently, in C#, each class or struct member must explicitly declare its access modifier (e.g., public class Example
{
public int PublicField1;
public int PublicField2;
public void PublicMethod1() {}
public void PublicMethod2() {}
private int PrivateField1;
private int PrivateField2;
private void PrivateMethod1() {}
private void PrivateMethod2() {}
} The repetition of By introducing Multiple Access Specifier Blocks, developers can group members under a single access level, reducing repetition and improving clarity. Detailed DesignThe proposed syntax mirrors that of C++, where access specifiers are defined as blocks with label-like modifiers: public class Example
{
public:
int PublicField1;
int PublicField2;
void PublicMethod1();
void PublicMethod2();
private:
int PrivateField1;
int PrivateField2;
void PrivateMethod1();
void PrivateMethod2();
} Proposed Syntax
Example: public class Example
{
public:
int PublicField1;
int PublicField2;
void PublicMethod1();
void PublicMethod2();
private:
int PrivateField1;
int PrivateField2;
void PrivateMethod1();
void PrivateMethod2();
} This is equivalent to the current syntax: public class Example
{
public int PublicField1;
public int PublicField2;
public void PublicMethod1();
public void PublicMethod2();
private int PrivateField1;
private int PrivateField2;
private void PrivateMethod1();
private void PrivateMethod2();
} Combined ModifiersThe feature could also support combined modifiers, such as public class Example
{
private protected:
int ProtectedInternalField;
void ProtectedInternalMethod();
static public:
int StaticPublicField;
void StaticPublicMethod();
} This ensures flexibility while maintaining the benefits of grouped declarations. Rules and Constraints
Advantages
Implementation Considerations
ConclusionIntroducing Multiple Access Specifier Blocks in C# would enhance the language's expressiveness and make code more concise and readable. While this feature draws inspiration from C++, it aligns well with C#'s focus on clarity and developer productivity and I suppose it wouldn't need any Runtime or IL modification (just simple syntactic sugar) and wouldn't also interfere with goto labels. I hope we will finally consider this proposal. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Sorry, I really don't like this at all. void DoSomething() { } I don't want to have to scroll up the class to find out whether it is public or private. This can also lead to an accidental exposure of private members with a single |
Beta Was this translation helpful? Give feedback.
-
I’d love this, although maybe with a slightly different syntax.
IDEs will give you breadcrumbs so you’ll quickly see where you are. I find all the explicit access modifiers add noise when quickly scrolling through a file, scanning for a function I’m looking for. |
Beta Was this translation helpful? Give feedback.
#1021