Search code examples
c#extension-methodsstatic-classes

Why can't I create extension methods for static classes?


When I try to create an extension method for the File class, I get an error telling me that I cannot do this because the class is static. However, I don't see why this stops the creation of an extension method, what implication is there?

Thanks


Solution

  • Extension methods are called on an instance of an object.

    myObj.ExtensionMethod();
    

    If you have a static class, you can't have an instance of it. Therefore, there's nothing to call the extension method on.