Why extension methods in C # class libraries

I looked through the documentation

+7
source share
4 answers

Extension methods as a name involve expanding the functionality or usability of the target type.

  • These methods can be added later (than the time the type was created) after the type is already published.
  • They can be written by different groups of people.
  • Extension methods can target interfaces. (An alternative would be to have a common base type having these methods or reimplementation in each type).
  • Different people can distribute the same type in different ways according to their needs.

The proper use of extension methods can remove orthogonal clutter from the actual type definition / implementation (instead of focusing the main type functions in the type definition).

Take LINQ as an example — by providing extension methods to IEnumerable , it can target a huge number of types already published (and a huge number of types that may be written in the future); it separates an orthogonal problem, such as a type request from an actual type.

+1
source

Note that many of these methods work on interfaces, which is a legitimate excuse for using extension methods, even for Microsoft, because the interfaces themselves cannot implement any methods.

+3
source

Documented extension methods are defined in IEnumerable<T> , which are implemented by ObjectSet<T> .

They are documented, so you know you can use them.

As extension methods, they ultimately extend any type that implements this interface for free.

+1
source

I think this is most likely a "marketing problem." A way to advise BCL consumer (us) to use extension methods where we need them.

In terms of usability, there is no kindness, imo, integrating them into BCL

0
source

All Articles