Why can’t my public class extend an internal class?

UPDATE: This question was the subject of my blog on November 13th of 2012. See it for some more thoughts on this issue. Thanks for the great question!


You’re right; it doesn’t have to be that way. Other OO languages allow “private inheritance”, whereby the fact that D inherits from B can only be taken advantage of by code that has the ability to see B.

This was a design decision of the original C# designers. Unfortunately I am away from my desk right now – I’m taking a couple of days off for the long weekend – so I don’t have the language design notes from 1999 in front of me. If I think of it when I get back I’ll browse them and see if there is a justification for this decision.

My personal opinion is that inheritance should be used to represent “is a kind of” relationships; that is, inheritance should represent the semantics of the domain being modelled in the language. I try to avoid situations where inheritance is used as a code sharing mechanism. As others have mentioned, it’s probably best to prefer composition to inheritance if what you want to represent is “this class shares implementation mechanisms with other classes”.

Leave a Comment