Type Inheritance?
I've come across something I find disturbing in C#.
Let's begin with an example:
#region Case 1
public class MyBaseClass
{
public static void MyStaticMethod()
{
// Work Some Magic
}
}
public class MyDerivedClass : MyBaseClass
{
public static void MyStaticMethod() // I will get a warning for not using 'new'
{
// Work Some Other Magic
}
}
#endregion
#region Case 2
public class MyOtherDerivedClass : MyBaseClass
{
}
public class SomeOtherClass
{
public int Main(string[] args)
{
MyOtherDerivedClass.MyStaticMethod(); // This would pass without a hitch!
}
}
#endregion
I have looked through the specs and it seems this is actually correct behaviour.
Why do my static members get inherited to the derived type? Does anyone have a good reason for this or knows the fact of why this came to be?
[ Update: Paul Bartlett and Josh Flanagan note that this behvaiour is C# specific and that when calling a 'derived' static member on a derived type, the end-result IL will call the base class. That's just another reason why I think this doesn't make sense. ]
Tip: View this page in IE and try clicking on the region directives. I'm now trying to get the script to work with Mozilla as well...
[
Update: Code now works for Mozilla users (that's what happens when you use IE's non-standard implicit document.all object.]