Why is it considered that the method is hidden if the parent and child classes have a static method with the same name?

If you declare an inheritance hierarchy where the parent and child classes have a static method with 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 method hiding? None of the methods are involved in the inheritance hierarchy and can only be called using the class name:

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

or, unskilled in the class itself. In any case, there is no ambiguity as to which method is being called (ie, "Hide").

* Interestingly, the methods may differ in return type and still generate the same warning. However, if the types of method parameters differ, a warning is not generated.

Note that I'm not trying to create an argument or discuss static inheritance or any other such stupid thing.

+4
source share
4 answers

SubClass members will not be able to access DoSomething from BaseClass without explicitly specifying the class name. Thus, it is effectively "hidden" to members of SubClass, but is still available.

For instance:

 public class SubClass : BaseClass { public static void DoSomething() { } public static void DoSomethingElse() { DoSomething(); // Calls SubClass BaseClass.DoSomething(); // Calls BaseClass } } 
+2
source

Why is this method hiding? None of the methods are involved in the inheritance hierarchy and can only be called using the class name.

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

 public Class A { public static void C() {...} } public Class B: A { } BC() // Valid call! 

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

Hope this helps.

+3
source

This is just a warning. The compiler just wants to make sure that you intentionally used the same method name.

0
source

Visual Studio and Philippe report this warning, so your code will compile and run.

However, "CodeNaked" perfectly demonstrates why it is hidden. This code compiles without 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 code, I can do the following:
BaseClass.DoSomething ();
SubClass.DoSomething ();

And it works great. The fact is, you may wonder why SubClass inherits from BaseClass, and both implement the same static methods. Actually, this is not so, both classes implement methods that can be completely different, but have the same name. This can be confusing.

0
source

All Articles