Why can’t interfaces specify static methods?

Suppose you could specify in an interface that a type had to have a particular static method… how would you call it? Polymorphism works through instances – whereas static members explicitly don’t use instances.

Now, having said that, there’s one situation in which I can see static interface members working: generic types. For example:

// This isn't valid code...
public void Foo<T>() where T : ICodeGenerator
    string type = T.GetDbConnectionType();

That would call the static member on the concrete type T.

I’ve blogged more about this, but I suspect the benefit doesn’t justify the complexity.

In terms of alternatives – usually you’d have another interface, and have separate types to implement that interface. That works well in some contexts, but not in others.

Leave a Comment