Is there a way to limit the access to a protected field in the class, to the
Asked
Active
Viewed 95 times
1
-
2You could just make it `internal`...if you're willing to deal with the fact that only classes inside your assembly will be able to see it. Depends on what you need. – rory.ap Mar 17 '17 at 12:03
-
@rory.ap you're correct. – Ousmane D. Mar 17 '17 at 12:04
-
here is a link to the access specifiers which you can use ---> http://stackoverflow.com/questions/585859/what-is-the-difference-between-protected-and-protected-internal – Ousmane D. Mar 17 '17 at 12:07
-
@rory.ap you are right, I misread what OP wants. – Evk Mar 17 '17 at 12:08
-
1The CLR supports it, the C# and VB.NET languages do not. You'll have to use `sealed`. – Hans Passant Mar 17 '17 at 12:08
-
@HansPassant but OP has protected _field_, how sealed would help? – Evk Mar 17 '17 at 12:09
-
Thank you I'm quoting the link " protected internal means "protected OR internal" I'm looking for protected AND internal. – Yinon Dotan Mar 17 '17 at 12:09
-
@Evk - a protected field is accessible only to a derived class. `sealed` prevents deriving a class. It is the courageous and often correct choice, doesn't get used enough. The language designers encouraged courage. – Hans Passant Mar 17 '17 at 12:13
-
Out of curiosity, what is the requirement to design like this? – Anil Mar 17 '17 at 12:47
-
Good question ,Same reason why we design with protected , just keeping it internal to better control the design. – Yinon Dotan Mar 17 '17 at 14:18
1 Answers
1
There's a lot information on SO regarding this exact topic.
While protected internal
means protected OR internal, you need protected AND internal (this or this questions for reference).
This is technically allowed on CLR level, but not supported in C# - see this question for example.
Looking at this question you can see plans for private protected
- something like you need.
Here is some technique to workaround this C# limitation.

Lanorkin
- 7,310
- 2
- 42
- 60