The C# spec states:
The declared accessibility of a base class member does not control
whether the member is inherited–inheritance extends to any member
that isn’t an instance constructor,static constructor,or destructor.
However, an inherited member may not be accessible in a derived type,
either because of its declared accessibility or because it is hidden
by a declaration in the type itself.
Why is an inaccessible member considered inherited? Why is such a distinction made/practical?
As a concrete example
class A
{
const string foo = "c";
internal void DoWork() { }
}
class B: A
{
const string bar = "d";//renamed to foo does not appear to have a noticeable impact
B() { bar = foo; }//inaccessible due to protection level
internal new void DoWork() { }//hide inherited member
}
In my mind, at runtime inheritance means sharing state and/or behavior. In the case of foo such a thing does not occur.
It is up to B as to whether or not to inherit the behavior of DoWork(). Therefore, DoWork() being a member of B is intuitive and relevant. On the other hand, why is foo treated as a Member of B? B cannot read from or write to foo.
In your case, you’re talking about
const, which is implicitly static. Static members effectively aren’t inherited anyway. Having just checked the spec, that implies that static members are inherited – but as they don’t represent state and can’t be part of polymorphism, it’s at least a “different” kind of inheritance. I think of static members as merely being “available via simple name” to derived classes, assuming they’re accessible at all – in other words, it’s to do with name resolution more than real inheritance.If this were a private instance variable, then it would be part of the state of any instance of an instance of any type derived from
A, so it would be inherited state. If you think about an object’s state and behaviour being inherited, it makes sense in my view.(It’s probably worth being clear about whether you’re interested in the static part or the private part; they’re somewhat orthogonal.)