Extension method by type

Is there a way to create an extension method for a type? It seems I can only 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


source to share


3 answers


No, It is Immpossible. Extension methods can only be created for instances



+5


source


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



+2


source


Correct answer: No. Extension methods are methods for instances of a certain class.

You may have more information from MSDN

0


source







All Articles