*** Welcome to piglix ***

Extension method


In object-oriented computer programming, an extension method is a method added to an object after the original object was compiled. The modified object is often a class, a prototype or a type. Extension methods are features of some object-oriented programming languages. There is no syntactic difference between calling an extension method and calling a method declared in the type definition.

Eric Lippert, a principal developer on the C# compiler team, says "Extension methods certainly are not object-oriented."

Extension methods are features of numerous languages including C#, Gosu, Javascript, Oxygene, Ruby, Smalltalk, Kotlin and Visual Basic.NET. In dynamic languages like Python, the concept of an extension method is unnecessary because classes can be extended without any special syntax (an approach known as "monkey-patching", employed in libraries such as gevent).

In VB.NET and Oxygene, they are recognized by the presence of the "extension" keyword or attribute.

In C# they're implemented as static methods in static classes, with the first argument being of extended class and preceded by "this" keyword.

In Smalltalk, any code can add a method to any class at any time, by sending a method creation message (such as methodsFor:) to the class the user wants to extend. The Smalltalk method category is conventionally named after the package that provides the extension, surrounded by asterisks. For example, when Etoys application code extends classes in the core library, the added methods are put in the *etoys* category.

In Ruby, like Smalltalk, there is no special language feature for extension, as Ruby allows classes to be re-opened at any time with the class keyword, in this case to add new methods. The Ruby community often describes an extension method as a kind of monkey patch.


...
Wikipedia

...