Why does Microsoft use extension methods for its own classes?

Why does Microsoft use extension methods for the classes it creates; instead of just adding methods to classes or creating child classes?

+6
source share
5 answers

There are a number of reasons why Microsoft did this. The two biggest creatures:

  • Extension methods apply to interfaces, not just classes. If Microsoft simply added Linq methods directly to IEnumerable, any concrete implementation of this interface would be required to implement these methods. By creating their extension methods written in terms of the existing IEnumerable <> behavior, each IEnumerable <> class gets them automatically.

  • For frameworks 3.0 and 3.5, the main System.dll file is the 2.0 library. Everything new in version 3.0 ad 3.5 was added on top of this in System.Core or other related libraries. The only way to get, for example, a new method in the List <class, which exists in version 3.5 but not in version 2.0, is to make the extension method available in library 3.5.

+12
source

IgnoreRoute() on RouteCollection is an extension method because it is intended to be used with the MVC infrastructure, and not with the main ASP.NET application. I assume that they will not want to pollute the RouteCollection class with methods that are not needed for applications other than MVC, but still allow MVC applications to use the class.

I'm not sure that this approach necessarily makes sense (because, for example, they could only create a child class); for more general reasons that extension methods can be used, others answered well.

+2
source

This is an example of a non-virtual interface template (similar to the Template Method ). This is a template used in languages ​​with multiple (implementation) inheritance, and the way to do this in C # is to use extension methods.

The main idea is that you have only non-virtual and purely virtual (abstract) methods. The interface designer (or the heir of the / mixin class, in languages ​​with multiple inheritance) implements a small method or set of methods (in this case GetEnumerator), and in return receives a whole set of methods that depend on this one abstract method (for example, Select, Where, Aggregate etc.)

As Michael Edenfield said in his answer, if we want to implement this template and we want IEnumerable to be an interface, we need to use extension methods. And to make IEnumerable in an abstract class would be bad, because it had to be a very inexpensive base interface that should be applied to almost any collection - IEnumerable implementation should not require rethinking the class hierarchy, it should be almost "free".

0
source

I think the main reason is that it is very easy to distribute such methods. For example, if you use Linq extension methods, you can easily write your own set of extension methods (possibly foreach or some specific filter) that will work using microsoft methods: mylist.Where (...). MyFilter (...). Select (...)

0
source

My two cents:

since extension methods were only added in later versions of the .NET platform, they already had the .NET Framework 1, 1.1, 2.0 and newer, and then at some point they added extension methods to use them to enrich the feature set on top of existing classes.

-1
source

Source: https://habr.com/ru/post/894386/


All Articles