Karsten
Karsten

Reputation: 8144

Extension method on type

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();
    }
}

Upvotes: 2

Views: 170

Answers (3)

AndreyAkinshin
AndreyAkinshin

Reputation: 19011

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

You can have more information from MSDN

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292345

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

Upvotes: 5

user151323
user151323

Reputation:

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

Upvotes: 2

Related Questions