Extension method

Extension methods are features of numerous languages including C#, Java via Manifold or Lombok or Fluent, Gosu, JavaScript, Oxygene, Ruby, Smalltalk, Kotlin, Dart, Visual Basic.NET, and Xojo.

The Smalltalk method category is conventionally named after the package that provides the extension, surrounded by asterisks.

The predominant reason why extension methods were introduced was Language Integrated Query (LINQ).

However, extension methods allow features to be implemented once in ways that enable reuse without the need for inheritance or the overhead of virtual method invocations, or to require implementors of an interface to implement either trivial or woefully complex functionality.

After carefully implementing the conversions and factories, switching from one class library to another can be made as easy as changing the using statement that makes the extension methods available for the compiler to bind to.

An example is Microsoft's Entity Framework configuration API, which allows for example to write code that resembles regular English as closely as practical.

The following example uses Entity Framework and configures the TodoList class to be stored in the database table Lists and defines a primary and a foreign key.

Even worse, this would have required everybody besides Microsoft considering to use IEnumerable themselves to also implement all those methods, which would have been very anti-productive seeing the widespread use of this very common interface.

With generic classes, extension methods allow implementation of behavior that is available for all instantiations of the generic type without requiring them to derive from a common base class, and without restricting the type parameters to a specific inheritance branch.

This is a big win, since the situations where this argument holds require a non-generic base class just to implement the shared feature - which then requires the generic subclass to perform boxing and/or casts whenever the type used is one of the type arguments.

A note should be placed on preferring extension methods over other means of achieving reuse and proper object-oriented design.

Extension methods might 'clutter' the automatic completion features of code editors, such as Visual Studio's IntelliSense, hence they should either be in their own namespace to allow the developer to selectively import them or they should be defined on a type that is specific enough for the method to appear in IntelliSense only when really relevant and given the above, consider that they might be hard to find should the developer expect them, but miss them from IntelliSense due to a missing using statement, since the developer may not have associated the method with the class that defines it, or even the namespace in which it lives - but rather with the type that it extends and the namespace that type lives in.

In programming, situations arise where it is necessary to add functionality to an existing class—for instance by adding a new method.

This includes sealed class and the different primitive data types in C# such as int, float and string.