简体   繁体   中英

Why is it considered method hiding if a parent and child class have a static method with the same name?

If you declare an inheritance hierarchy where both the parent and child class have a static method of the same name and parameters*, Visual Studio will raise warning CS0108 :

Example:

public class BaseClass
{
    public static void DoSomething()
    {
    }
}

public class SubClass : BaseClass
{
    public static void DoSomething()
    {
    }
}

: warning CS0108: 'SubClass.DoSomething()' hides inherited member 'BaseClass.DoSomething()'. Use the new keyword if hiding was intended.

Why is this considered method hiding? Neither method is involved in the inheritance hierarchy and can only be invoked by using the class name:

BaseClass.DoSomething();
SubClass.DoSomething();

or, unqualified in the class itself. In either case, there is no ambiguity as to which method is being called (ie, no 'hiding').

*Interestingly enough, the methods can differ by return type and still generate the same warning. However, if the method parameter types differ, the warning is not generated.

Please note that I am not trying to create an argument for or discuss static inheritance or any other such nonsense.

Why is this considered method hiding? Neither method is involved in the inheritance hierarchy and can only be invoked by using the class name.

That is not true. You can call DoSomething from any inherited class name:

public Class A
{
     public static void C() {...}
}

public Class B: A
{

}

B.C() // Valid call!

That is why you are hiding C() if you declare the method with the same signature in B.

Hope this helps.

Members of the SubClass will not be able to access the DoSomething from BaseClass without explicitly indicating the class name. So it is effectively "hidden" to members of SubClass, but still accessible.

For example:

public class SubClass : BaseClass
{
    public static void DoSomething()
    {
    }

    public static void DoSomethingElse()
    {
        DoSomething(); // Calls SubClass
        BaseClass.DoSomething(); // Calls BaseClass
    }
}

It's just a warning. The compiler just wants to make sure you intentionally used the same method name.

Visual Studio, and Philippe, are saying it's a warning so your code will compile and run.

However, 'CodeNaked' nicely demonstrates why it is hidden. This code compiles without throwing errors or warnings. Thanks to 'CodeNaked'

public class BaseClass {
    public virtual void DoSomething() {
    }
}

public class SubClass : BaseClass {
    public override void DoSomething() {

    }

    public void DoSomethingElse() {
        DoSomething(); // Calls SubClass
        base.DoSomething(); // Calls BaseClass
    }
}

EDIT: With Travis's code I can do the following:
BaseClass.DoSomething();
SubClass.DoSomething();

And it works fine. Thing is though you might wonder why SubClass is inheriting from BaseClass and both are implementing the same static methods. Actually that's not true, both classes are implementing methods that could be completely different but have the same name. That could be potential confusing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM