Bartolinio
Bartolinio

Reputation: 790

Implement extension method interface C#

I am newbie to C#, and have a problem with understanding how should I implement an extension method of interface. I didn't found any material about this issue. From the material I found about general extension methods in C# I expected the following simple demonstrating example work:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testExtension
{
public  interface  IcheckThingsOut
{
   bool x();
}
public static class extensionInterface
{
   public static bool y(this IcheckThingsOut bla) { return true;}
}
public class implementInteface : IcheckThingsOut
{
   bool IcheckThingsOut.x()
   {
       return true;
   }
   bool IcheckThingsOut.y()
   {
       return false;

   }
}
}

But the compiler is still not satisfied. What did I miss, and what is the right syntax here (with the same semantics as the code)?

Upvotes: 2

Views: 3434

Answers (5)

jeroenh
jeroenh

Reputation: 26792

You can not change the implementation of an extension method inside the "extended" class or interface. The extension method IS the implementation.

Extension methods provide a way to "add" methods to an interface or a class. The whole idea is that the interface or class you are extending does not "know" about these methods. In other words, you "add" functionality without altering the class or interface itself.

(Note that while the notation suggests that your are adding methods to the interface, in fact these methods are plain static methods as far as the runtime is concerned).

Upvotes: 2

Jay
Jay

Reputation: 57959

What you seem to be looking to use is "extension methods". Your extensionInterface class is properly implemented for this purpose.

However, creating an extension method does not actually extend your interface, so although you can call myIcheckThingsOut.y(), you cannot re-implement the method in a class that implements IcheckThingsOut, implicitly or explicitly.

Upvotes: 3

m0sa
m0sa

Reputation: 10940

You have messed up the concepts of extension methods and interfaces. Extension methods are just a compiler convenient way of calling static methods on static classes. They do not extend the interface. What the compiler does when using an extensions method is:

  IcheckThingsOut instance = new IcheckThingsOutImpl();
  instance.y();

... will be converted to:

  IcheckThingsOut instance = new IcheckThingsOutImpl();
  extensionInterface(instance, y);

So as you can see, y is not a method on your interface. This is why you can't implement it explicitly in the implementation.

Upvotes: 3

Jacob
Jacob

Reputation: 995

bool IcheckThingsOut.y()
{
    return false;

}

is wrong: IcheckThingsOut provides no prototype for function y. It should just be

bool y()
{
    return false;
}

since it's just shadowing the other method

Upvotes: 1

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

The IcheckThingsOut dosn't have bool y()

Upvotes: 0

Related Questions