What is so good about extension methods?

Possible duplicate:
What are the benefits of extension methods you find?

All rights, first of all, I understand that this sounds debatable, but I do not want to be confrontational. I ask a serious question out of real curiosity (or maybe puzzling is the best word).

Why were .NET methods implemented in .NET? What benefits do they provide besides the fact that everything looks good (and “good” I mean “deceptively, like instance methods”)?

For me, any code that uses the extension method as follows:

Thing initial = GetThing(); Thing manipulated = initial.SomeExtensionMethod(); 

It is misleading, as it implies that SomeExtensionMethod is a member of Thing , which misleads the developers in faith (at least as a feeling of the gut ... you can deny it, but I definitely watched it) that (1) SomeExtensionMethod is likely to be implemented effectively, and (2) since SomeExtensionMethod really looks like part of the Thing class, it will certainly remain valid if Thing is revised at some point in the future (as long as the author of Thing knows that he / she does).

But the fact is that extension methods do not have access to protected members or to any internal operation of the class that they extend, therefore they are as prone to breakage as any other static methods.

We all know that the above can be easy:

 Thing initial = GetThing(); Thing manipulated = SomeNonExtensionMethod(initial); 

I think this is much more, due to the lack of a better word, honest .

What am I missing? Why are there extension methods?

+4
source share
6 answers

Extension methods were needed to get Linq to work in a clean way that it does with a chain of methods. If you need to use a "long" form, this leads to the fact that function calls and parameters will be separated from each other, which makes the code very difficult to read. For comparison:

 IEnumerable<int> r = list.Where(x => x > 10).Take(5) 

vs

 // What does the 5 do here? IEnumerable<int> r = Enumerable.Take(Enumerable.Where(list, x => x > 10), 5); 

Like everyone else, they can be abused, but extension methods are really useful when used properly.

+14
source

I think the main potential is openness. Enter initial and a point, and there you have everything you can do with it. It's much harder to find static methods hidden in some class elsewhere.

+6
source

First of all, in the case of Thing manipulated = SomeNonExtensionMethod(initial); SomeNonExtensionMethod is based on the same assumptions as in the case of Thing manipulated = initial.SomeExtensionMethod(); . The thing may change, SomeExtensionMethod may break. This life is for us programmers.

Secondly, when I see Thing manipulated = initial.SomeExtensionMethod(); , it does not tell me exactly where SomeExtensionMethod () is implemented. Thing can inherit it from TheThing, which inherits it from TheOriginalThing. Thus, the "misleading" argument leads to nowhere. I bet the IDE cares about leading you to the right source, right?

What is so cool? This makes the code more consistent. If it works with a string, it is like membership in a string. It's ugly to have several MyThing.doThis() methods and several static ThingUtil.doSomethingElse(Mything thing) methods in another class.

+3
source

SO, you can extend the class of another. not yours ... this is an advantage. (and you can say .. oh, I want them to implement this / that ... you just do it yourself ..)

0
source

they are great for automatically mixing functionality based on the interfaces that the class inherits if this class does not have to explicitly implement it.

Linq uses this a lot.

A great way to decorate classes with extra functionality. Most effective when applied to an interface, not to a specific class. However, a good way to extend the classes of the Framework.

0
source

It's just a convenient syntactic sugar so you can call a method with the same syntax, regardless of whether it is really part of the class. If member A releases lib and member B releases material that uses this library, it’s easier to just name everything with class.method (args) than remember what is called using the (class, args) vs. class.method (arg).

0
source

All Articles