Undocumented C# "Feature"

For the most part the access modifiers in C# are pretty straightforward to use. Public, protected, internal, and private each cause behavior that is obvious from the keyword and shouldn’t require looking up more than once.

The exception is the fifth modifier: protected internal, which allows access to anything that falls inside the scope of the protected OR internal behaviors. Wouldn’t it be nice to have a modifier that provided the same behavior but instead restricted it to protected AND internal (only accessible to derived classes in the same assembly)? And what would happen if you couldn’t remember the order of “protected internal” and accidentally used “internal protected” instead? This does actually compile just fine and you might think it could provide the AND behavior, adding some flexibility to the language. But alas, the switched order does the same thing as the documented “protected internal” modifier.

So the good news is that you don’t need to remember the order, the bad news is that there’s no extra functionality and it can cause even more confusion than just the normal behavior itself.

Leave a Reply

Your email address will not be published. Required fields are marked *