1

Possible Duplicate:
Why Doesn’t C# Allow Static Methods to Implement an Interface?

I have code like this that says I can't implement a static methods:

public static class AuxiliaryHelper : IAuxiliaryHelper
{
    /// <summary>
    /// Writes the response.
    /// </summary>
    /// <param name="jsonObj">The json object that gets turned in JSON and written out.</param>
    public static void WriteResponse(this object jsonObj)
    { ....

Can I get an interface for this?

Community
  • 1
  • 1
cdub
  • 24,555
  • 57
  • 174
  • 303

3 Answers3

3

Short answer: No.

Longer answer:

This concept doesn't really make any sense. The point of an interface is to define a base type that describes a contract for various implementations. I can declare a variable using a static type of IFoo, which tells the compiler I don't know what type this will be at runtime, but I assure you it will have a certain set of methods. - Thus, the compiler will let you call those methods, which will be resolved at runtime.

A static method is always bound to one and only one class, so there's no instance to refer to.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • I disagree a bit with "completely useless" - from time to time I encounter cases where I have different types and I would like to query the types for some information, for example for supported capabilities that are not easily expressed by implementing interfaces or adding attributes, without the need to create an instance for this. I usually solve this by creating separate types with the sole purpose of describing the other types. – Daniel Brückner Jan 28 '13 at 23:09
  • @DanielBrückner - That might make sense if you were passing a reference to a class itself around. I think that would be a totally different use-case than interfaces, which allow an object's static type to be different than its dynamic type. – Mike Christensen Jan 28 '13 at 23:11
  • Not exactly what I was up to. For example I once created a visual designer that allowed to build data processing pipelines. Each building block was implemented as a class with some interfaces - one block for adding two numbers, one block for multiplex two inputs, ... I would really have liked it if could have extracted information about each building block via static interfaces instead of creating descriptor classes or throw away instances because this information is needed before the user places an instance on the design surface. – Daniel Brückner Jan 28 '13 at 23:20
  • @DanielBrückner - Yea, that's what metadata is for. Frameworks such as web parts, Active Record, web services, etc - all use metadata attributes to describe this sort of type information. – Mike Christensen Jan 28 '13 at 23:25
  • Yep, but sometimes strings, and integers and so on are just not expressive enough. It is definitely not to hard to work around this but from time to time static interface would really by handy. – Daniel Brückner Jan 28 '13 at 23:33
2

No, interfaces can't have static methods. A class that implements an interface needs to implement them all as instance methods. Static classes can't have instance methods.

DadViegas
  • 2,263
  • 16
  • 12
0

I noticed, you use an extension method. Perhaps this variation can help you.

Meta code

public static class HelperExtensions
{
    //Note the interface goes here 
    public static void WriteResponse(this IAuxiliaryHelper helper, 
                                     object jsonObj)
    { 
        //omn nom nom
    }
}

//somewhere else - usage
IAuxiliaryHelper h = new Foo();
h.WriteResponse(new JsonObject());
oleksii
  • 35,458
  • 16
  • 93
  • 163