|
Like all other language features, I use them when it's appropriate.
That means sometimes I do, and sometimes I don't. It depends on what the method is doing and whether it makes more sense as an extension method or as a property / standard method.
If you need to add functionality to a sealed class such as String then it makes a lot of sense. If the class is your own, then it's just silly to use extension methods ...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
OriginalGriff wrote: If the class is your own, then it's just silly to use extension methods No it's not!
On a few occasions I've created an interface, ISomething, and before writing any implementation I wrote a few extension methods I knew I needed.
And then I wrote the implementations (and got so much functionality out of the box!)
Kind of like collections and LINQ, all M$ "own" classes, but they still have lots of extension methods.
|
|
|
|
|
Hi Sandor, I'd really appreciate seeing an example of Extension Methods for an Interface.
cheers, Bill
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
The entire LINQ library depends upon it.
For example the Count() extension method on collections may look as follows:
public static class Extensions
{
public static int Count<T>(this IEnumerable<T> collection)
{
int count = 0;
using (var enumerator = collection.GetEnumerator())
{
while (enumerator.MoveNext())
{
count += 1;
}
}
return count;
}
} The actual extension method checks for null and tries to cast to ICollection<T> and ICollection for the Count property first, but it's an extension on an interface.
There are LOTS of them...
|
|
|
|
|
thanks for this example!
diligent hands rule....
|
|
|
|
|
Sander Rossel wrote: On a few occasions I've created an interface, ISomething, and before writing any implementation I wrote a few extension methods I knew I needed.
Still doesn't make sense to me. If the interface is your own, you should write the method to the interface as it makes sense to be in its scope. If you need behavior without having to implement everywhere, you're looking for an abstract class.
Extension methods provide a clean way to have helper methods to class you cannot modify. Otherwise you're just creating spaghetti code and killing a few principles.
Although M$ does with it's own classes, it makes sense, specially to not break backwards compatibility when implementing new frameworks (like LINQ), so yes, they own the code, but they should not modify it, as LINQ is implemented in a whole different scope.
If you are developing frameworks, with attachable components, that extend other standalone components, yes, it makes sense to have extension methods on your own code, otherwise I agree that's "silly".
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson
Our heads are round so our thoughts can change direction - Francis Picabia
|
|
|
|
|
I wholeheartedly disagree
Extension methods are nothing more than static helper methods that are treated as instance methods by the editor (and the compiler?).
I don't think there is a principle against creating static helper methods and it's certainly not creating spaghetti, especially since the methods read as instance methods (editor trick or not).
An abstract class is heavy weaponry, every implementor must now inherit the abstract class and since you can only inherit once this puts a serious limitation on implementors.
If you can't inherit you don't get the functionality. You could create a wrapper, but that's just a lot of boilerplate code.
Besides, inheritance is added complexity/spaghetti too!
public interface ISomething
{
IEnumerable<object> Stuff { get; }
}
public static class SomethingExtensions
{
public static bool HasStuff(this ISomething something)
{
return something.Stuff.Any();
}
} How is this not awesome? Every implementor now gets HasStuff for free without the need for inheritance!
You agree that extension methods are clean for LINQ and when dealing with third party code, but for your own code it's suddenly spaghetti? Sounds like double standards to me
modified 18-Oct-16 3:38am.
|
|
|
|
|
As OG pointed out when adding functionality to sealed class then yes, but otherwise I consider between extension methods and inheritance. Mainly because I often need more functionality than just a method and perhaps even new instance variables.
But yes I use them when I see fit.
|
|
|
|
|
I'm sure the OP means extension methods for native .Net objects, such as string , Type , DateTime , etc.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Yep, that's what I understood.
|
|
|
|
|
I use them a lot to encapsulate the business meaning of a specific IEnumerable<t> Where clause. Makes code much more readable.
|
|
|
|
|
My most recent ones calculate business chart trend line values from either an IEnumerable<double> or a IEnumerable<T> using the named property.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
this is a good one..
diligent hands rule....
|
|
|
|
|
Yes I do. I use the ones MS created, I use them to enhance built-in types and I use them to enhance my own types.
Only when it makes sense, of course
|
|
|
|
|
I mostly use them for things that can't have methods defined on them directly, interfaces and enums.
|
|
|
|
|
For some reason I never thought of writing them for enums, though I use them for interfaces all the time. Can you give an example where an extension method for an enum would be useful? Can't think of any myself off hand.
|
|
|
|
|
Well it depends on what that enum is about, it's not always useful. I've used it for example to give an enum ConditionCode { etc the fake-methods Invert and SwapOperands .
|
|
|
|
|
|
public static bool Yes<T>(this T foo) {return true;}
Marc
|
|
|
|
|
Samhain (Halloween) is still fifteen days away, but you got my vote on this one, anyhow.
cheers, Bill
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
very inspirational
diligent hands rule....
|
|
|
|
|
Marc Clifton wrote: public static bool Yes<T>(this T foo) {return true;} That looks suspiciously like an advert for Ty-phoo tea!
I wanna be a eunuchs developer! Pass me a bread knife!
|
|
|
|
|
I use them whenever they make the code clearer & more concise, which is often.
Intensively? Probably not.
Extensively? Maybe.
|
|
|
|
|
Extensively, yes. Intensively, no.
- I would love to change the world, but they won’t give me the source code.
|
|
|
|
|
We use them in order to have POCO's and then attach extension methods to them to give them some mojo.
cheers
Chris Maunder
|
|
|
|