Can extension methods be applied to interfaces?

Posted by Greg on Stack Overflow See other posts from Stack Overflow or by Greg
Published on 2010-05-05T02:56:16Z Indexed on 2010/05/05 3:08 UTC
Read the original article Hit count: 267

Filed under:
|
|
|

Hi,

Is it possible to apply an extension method to an interface? (C# question)

That is for example to achieve the following:

  1. create an ITopology interface

  2. create an extension method for this interface (e.g. public static int CountNodes(this ITopology topologyIf) )

  3. then when creating a class (e.g. MyGraph) which implements ITopology, then it would automatically have the Count Nodes extension.

This way the classes implementing the interface would not have to have a set class name to align with what was defined in the extension method.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET