Edit: I have asked question to understand why C# designers chose it to behave in particular fashion?
Similar question has been asked, but this is little different.
Should following code give warning?
class Foo { public void Do() { /*...*/ } /*...*/ }
class Bar : Foo { public static void Do() { /*...*/ } /*...*/ }
It gives: "warning CS0108: 'Bar.Do()' hides inherited member 'Foo.Do()'. Use the new keyword if hiding was intended."
Let me make a change in code.
class Foo { public static void Do() { /*...*/ } /*...*/ }
class Bar : Foo { public void Do() { /*...*/ } /*...*/ }
Same warning.
If you do following, warning goes away.
class Foo { public void Do() { /*...*/ } /*...*/ }
class Bar : Foo { new public static void Do() { /*...*/ } /*...*/ }
Let me make further change.
class Foo { public void Do() { /*...*/ } /*...*/ }
class Bar : Foo {
new public static void Do()
{ new Bar().Do();/*...*/ } /*...*/
}
This does not compile. error CS0176: Member 'Bar.Do()' cannot be accessed with an instance reference; qualify it with a type name instead.
So, I lose access to inherited method via instance reference from static method!
What would be logic behind it? Or I made a typo somewhere?
Btw, I come across this when I was trying to define static method 'Show' for my form derived from 'Form'.