views:

75

answers:

3

Hi

Is there a way to create an extension method for an type ? I only seem to be able to create them for instances.

public static class MyExtensions
{
    public static string Test(this string s)
    {
        return "test";
    }
}

public class Test
{
    static void TestIt()
    {
        string.Test();  // won't compile

        string s = null;
        s.Test();
    }
}
+2  A: 

No. Extension methods are only for instances. In other words, it is not possible to have static extension methods.

Developer Art
Well, technically all extension methods are static... but I see your point ;)
Thomas Levesque
+4  A: 

No, it's not possible. Extension methods can only be created for instances

Thomas Levesque
why the down vote ??
Thomas Levesque
A: 

Right answer: No. Extension methods is methods for instances of some class.

You can have more information from MSDN

DreamWalker