Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Interface using extension methods in c#

I have an Interface:

public interface IMessager
{
    void ShowMessage();
}

Is there any way to implement this interface using extension methods?

public static class Extensions
{
  public static void ShowMessage(this MyClass e)
  {
      Console.WriteLine("Extension");
  }
}

and a class that implement it:

public class MyClass:IMessager
{
    public void ShowMessage()
    {
        ShowMessage(); // I expect that program write "Extension" in console
    }
}

But when I run the program I get the System.StackOverflowException.

like image 641
Masoud Avatar asked Mar 26 '14 10:03

Masoud


People also ask

Can an extension method implement an interface?

You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself.

What are extension methods explain with an example?

An extension method is actually a special kind of static method defined in a static class. To define an extension method, first of all, define a static class. For example, we have created an IntExtensions class under the ExtensionMethods namespace in the following example.

How do you implement and call a custom extension method?

To define and call the extension methodDefine a static class to contain the extension method. The class must be visible to client code. For more information about accessibility rules, see Access Modifiers. Implement the extension method as a static method with at least the same visibility as the containing class.


1 Answers

The code you posted is just a method calling itself recursively (hence the StackOverflowException).

I'm not entirely sure what you're trying to accomplish but to answer your question

Is there any way to implement this interface using extension methods?

No.

To be a bit more pragmatic about this though, if your aim is to only write your method once you have a few options:

1. Call the extension explicitly

public class MyClass:IMessager
{
    public void ShowMessage()
    {
        Extensions.ShowMessage(this);
    }
}

although as pointed out in comments, this basically defeats the point of using the extension method. Additionally there is still "boiler-plate code" such that every time you implement your interface you have to call the static method from within the method (not very DRY)

2. Use an abstract class instead of an interface

public abstract class MessengerBase
{
    public void ShowMethod() { /* implement */ }
}

public class MyClass : MessengerBase {}

...

new MyClass().ShowMethod();

This issue with this though is that you can't inherit from multiple classes.

3. Use extension on the interface

public interface IMessenger { /* nothing special here */ }

public class MyClass : IMessenger { /* also nothing special */ }

public static class MessengerExtensions
{
    public static void ShowMessage(this IMessenger messenger)
    {
        // implement
    }
}

...

new MyClass().ShowMessage();
like image 183
dav_i Avatar answered Sep 18 '22 22:09

dav_i