Search code examples
c#staticextension-methods

Why do extension methods need to be static?


I don't understand why it has to be static? What's the problem with being able to make an object for an extension method?


Solution

  • Extension methods are a shortcut to make it appear that a class has more methods than it actually does by allowing you to add a call to the extension method to a reference to an instance of the extended class. A non-static extension method would additionally require a reference to an instance of the extension class (as a non-static method may reference instance-specific state), and that wouldn't make sense in the syntax.

    If you want to be able to perform a method on instances of two classes, you can do that, just not in the way you envision.