0

How can i implement a method from a class that is extended to an Interface?

I have this Interface:

public Interface myInterface
{
      public static int myMethod();
}

And this class:

public class MyClass : myInterface
{
       // And I want here to implement the method form myInterface and i don't know how
}
3
  • 2
    An interface cannot have static methods. Commented Jan 1, 2012 at 20:13
  • 1
    You'll need to remove both the public and static modifiers on the myMethod declaration in the interface. Commented Jan 1, 2012 at 20:16
  • stackoverflow.com/questions/1812329/… Commented Jan 1, 2012 at 20:20

3 Answers 3

5

Interface won't compile - change it to interface. static can't be part of an interface.

After corrections the code would look like this for example:

public interface myInterface
{
      int myMethod();
}

public class MyClass : myInterface
{
       public int myMethod()
       {
           return 1;
       }
}

Regarding static and interface see a quote from http://channel9.msdn.com/forums/TechOff/250972-Static-Inheritance/?CommentID=274217 :

Inheritance in .NET works only on instance base. Static methods are defined on the type level not on the instance level. That is why overriding doesn't work with static methods/properties/events...

Static methods are only held once in memory. There is no virtual table etc. that is created for them.

If you invoke an instance method in .NET, you always give it the current instance. This is hidden by the .NET runtime, but it happens. Each instance method has as first argument a pointer (reference) to the object that the method is run on. This doesn't happen with static methods (as they are defined on type level). How should the compiler decide to select the method to invoke?

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for actually noticing the static in the method signature (and the issue with this).
The public in the interface will also be a problem.
0

You declared your interface incorrectly. Interface in C# specified with interface keyword, in that exact casing. Also interfaces cannot contain static methods. For further details please refer to Interfaces (C# Programming Guide) MSDN article.

Comments

-1

Second edit: shame on me. I gave this answer before I had tested it, then realized I had never declared a static method in an interface before. It isn't supported, there's an SO article here that covers it

disregard the answer below...

Simply implement the method as would normally:

public Interface myInterface
{
   static int myMethod();
}

//impl
public class MyClass : myInterface
{
   public static int myMethod()
   {
      //implementation here
   }
}

Edit: I had stated this would make the method callable from an instance of the abstract or concrete class, then realized I had forgotten it was an abstract method...

2 Comments

Interfaces simply cannot have static methods.
Indeed, hence the second edit explaining my mistake. Perhaps I'll move it to the top of the post...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.