You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Summary
A new accessibility keyword for members (or types) used within partial types similar to private except it limits accessibility to the "partial-block" in which it was defined.
Consider the example:
publicpartialclassTest{privateintx;voidValidTestMethod(){x++;}}//Somewhere else in our file or in a different file:partialclassTest{voidBrokenTestMethod(){x++;}}
So far this would still be allowed, but image we replaced private with a new keyword (lets call it private blockonly for now, I'll gladly take suggestions for what the best name for the keyword would be) such that our x field looks like this:
privateblockonlyint x;
The increment (the reference to x) in our BrokenTestMethod method would generate an accessibility compiler error because x is defined outside of the current "partial-block"
But our ValidTestMethod won't because x is defined within the same "partial-block".
The same applies to any member defined in a partial type (including types)
A class for instance would just simply be defined as follows:
partialclassTest{privateblockonlyclass Test2 {}}
The existing behaviour of private (both in partial types and non-partial types) will of course remain the same
Our new keyword would of course be stackable with other keyword if the user so desires such as:
privateblockonlyreadonlyrefstructTestStruct{}
just to name a long example.
Although it would NOT be stackable with other accessibility keywords like internal, protected, etc.
Motivation
I personally use partial classes a lot, having a part of my type in multiple files (or perhaps multiple places within 1 file) is my preferred way to organize my code. Some developers like making "base classes" or perhaps "static helpers classes" of which I do a fair bit too, but I could really get a lot of use of this new feature even while mixing some of those techniques in.
Like for instances if I write a method that needs fields, but those fields only gets accessed within that method or within that area of my code, blockonly would help to prevent polluting the rest of my type when writing features or methods within that type.
Another Way is to append such fields with _ or even __ which is generally how I fix such a scenario, but that is a bit messy, and I believe this feature will help with this by a lot
Editor Alternatives (Perhaps?)
If anyone knows of an visual studio extension (perhaps?) that would do something similar I would be very interested in that.
No potential .Net Issues (As far as my understanding goes)
As far as my understanding goes, this should be possible even while following the .net specifications because when partial types get compiled they all get added into the same type as if they were never partial to begin with thus the behaviour of existing runtime accessibility checks will remain the same.
This is thus syntax sugar which won't be embedded anywhere once the code is compiled
Ending
This is my first time submitting a proposal, if I'm missing any information of if I have misunderstood the guidelines please let me know so I can correct it as far as possible.
Please feel free to comment what you think about this proposal, I'm interested to see what you people out there think of it
The text was updated successfully, but these errors were encountered:
Summary
A new accessibility keyword for members (or types) used within partial types similar to
private
except it limits accessibility to the "partial-block" in which it was defined.Consider the example:
So far this would still be allowed, but image we replaced
private
with a new keyword (lets call itprivate blockonly
for now, I'll gladly take suggestions for what the best name for the keyword would be) such that ourx
field looks like this:The increment (the reference to
x
) in ourBrokenTestMethod
method would generate an accessibility compiler error becausex
is defined outside of the current "partial-block"But our
ValidTestMethod
won't becausex
is defined within the same "partial-block".The same applies to any member defined in a partial type (including types)
A class for instance would just simply be defined as follows:
The existing behaviour of
private
(both in partial types and non-partial types) will of course remain the sameOur new keyword would of course be stackable with other keyword if the user so desires such as:
just to name a long example.
Although it would NOT be stackable with other accessibility keywords like
internal
,protected
, etc.Motivation
I personally use partial classes a lot, having a part of my type in multiple files (or perhaps multiple places within 1 file) is my preferred way to organize my code. Some developers like making "base classes" or perhaps "static helpers classes" of which I do a fair bit too, but I could really get a lot of use of this new feature even while mixing some of those techniques in.
Like for instances if I write a method that needs fields, but those fields only gets accessed within that method or within that area of my code, blockonly would help to prevent polluting the rest of my type when writing features or methods within that type.
Another Way is to append such fields with
_
or even__
which is generally how I fix such a scenario, but that is a bit messy, and I believe this feature will help with this by a lotEditor Alternatives (Perhaps?)
If anyone knows of an visual studio extension (perhaps?) that would do something similar I would be very interested in that.
No potential .Net Issues (As far as my understanding goes)
As far as my understanding goes, this should be possible even while following the .net specifications because when partial types get compiled they all get added into the same type as if they were never partial to begin with thus the behaviour of existing runtime accessibility checks will remain the same.
This is thus syntax sugar which won't be embedded anywhere once the code is compiled
Ending
This is my first time submitting a proposal, if I'm missing any information of if I have misunderstood the guidelines please let me know so I can correct it as far as possible.
Please feel free to comment what you think about this proposal, I'm interested to see what you people out there think of it
The text was updated successfully, but these errors were encountered: