Correnting My Mistake: Static Methods & Child Classes

time to read 3 min | 416 words

In a previous post I said that you can't "inherit" static methods from your parent class. Apperantly I was mistaken. The following code works on both 1.1 & 2.0:

public class Parent
{
 public static void Static()
 {
 }
}
public class Child : Parent
{
}
class MainClass
{
 static void Main(string[] args)
 {
  Child.Static();//The real method called here is Parent.Static();

 }
}

Thanks to Tomer Gabel for setting me straight on the subject. I'm not sure what exactly convinced me that you can't do that.

Anyway, this opens up a whole new set of options when you combine static generic methods. So the code I showed last time would work:

 public class ActiveRecordBase<T> where T : ActiveRecordBase
{
   public static T[] FindAll() { .... }
}

public class Blog : ActiveRecordBase<Blog> { ... } 

Blog [] blogs = Blog.FindAll(); 

This is another cool tool to keep in the toolbox.