I had an interesting problem today. I had a public class in my assembly called
Session. It was a public class, but it used a lot of internal stuff in the implementation. It went something like this:
Later I needed to split it into two classes:
TradeSession. They have a lot in common, but they need to handle responses slightly differently. I decided to keep shared functionality in
Session making it a base class. So, I modified Session like this:
I immediately ran into trouble: inconsistent accessibility,
Response class is less accessible then method
HandleResponse is now a protected method of a public class, so theoretically it can be overriden outside of the assembly.
I could make
Session an internal class, but public classes
MarketDataSession are not allowed to derive from an internal class.
The only solution that allows me to keep
Response class internal is to mark
HandleResponse method as internal. This makes it invisible from outside of the assembly, but now it is wide open inside the assembly, which is also not so stellar.
There is another, rarely used access control option,
protected internal, but it actually means protected OR internal, i.e. the method is accessible to anyone inside the assembly and to derived classes outside of the assembly.
What I need here is protected AND internal: accessible only to derived classes within the same assembly. AFAIK, this option exists in IL and in C++/CLI as
private protected . Here’s a real example where it would be useful in C#.