1

Consider the following classes:

public class A
{
}

public class B
{
     public static A StaticA { get; set; }
}

public class C : B
{
     public static A StaticA { get; set; }
}

This will throw a warning:

'C.StaticA' hides inherited member 'B.StaticA'. Use the new keyword if hiding was intended.

I can get rid of the warning by adding the new keyword as I am told:

public static new A StaticA { get; set; }

But why should I? Why are static members being inherited? As far as I'm concerned static members are bound to a specific type, not an instance.

However, the warning does say "inherited member", so these static properties seem to be in fact inherited.

Why is this? Isn't static inheritance against OOP principles?

Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
  • 1
    I don't think this is a duplicate. Maybe I should reformulate my question, but I am asking why the warning uses the word `inherited member` when in fact, static members **are not inherited**, accessing them from a child class is just syntatic sugar. – Matias Cicero Sep 18 '15 at 12:54

1 Answers1

1

It's just a poor choice of words in the warning. The property is in fact not inherited, but it is hidden in the sense that you can access the base property using the inheriting class's name.

Perhaps a better warning would be

'C.StaticA' hides base member 'B.StaticA'. Use the new keyword if hiding was intended.

Another reason could be that the same static analysis that detects hiding of inherited members also detects hiding of static members, and they just use the same message for both (not distinguishing between static and non-static members)

D Stanley
  • 149,601
  • 11
  • 178
  • 240