Extension methods or static methods?

Coordinator
Oct 22, 2007 at 9:57 PM
Which would you prefer DSA used for its standalone algorithms for sorting, searching etc?

Any feedback would be appreciated.

Granville.
Nov 19, 2007 at 10:36 AM
Extension methods, of course!

Ivan.
Coordinator
Nov 19, 2007 at 11:29 AM
Hi Ivan,

At the moment most of the algorithms are extension methods, however there are a few cases where they are static methods.

I have simply chosen the approach which provides most readability so if you spot something that you feel should be one or the other but isn't let me know and I will review the implementation to see if its clearer.

Thanks,

Granville.
Feb 21, 2008 at 4:34 PM
Hi Granville,

I fully embrace extension methods. Not only do they allow for the same invocation style as instance methods, but they can also be called in the same way as regular static methods, like so:

public static class Algorithms
{
public static IList<T> BubbleSort<T>(this IList<T> source) { ... }
}

var myList = new List<T> { 5, 2, 7, 1, 8 };
var sortedList1 = myList.BubbleSort();
var sortedList2 = Algorithms.BubbleSort(myList);

Since you get the best of both worlds, it makes little sense not to use extension methods :).

Cheers,
Mike
Coordinator
Feb 21, 2008 at 6:47 PM
True. I'll admit I didn't actually try the normal route until a week or so after starting this thread - but I would encourage the ext method way as it increases the synergy between the BCL and DSA.

Thanks Mike!


mstrobel wrote:
Hi Granville,

I fully embrace extension methods. Not only do they allow for the same invocation style as instance methods, but they can also be called in the same way as regular static methods, like so:

public static class Algorithms
{
public static IList<T> BubbleSort<T>(this IList<T> source) { ... }
}

var myList = new List<T> { 5, 2, 7, 1, 8 };
var sortedList1 = myList.BubbleSort();
var sortedList2 = Algorithms.BubbleSort(myList);

Since you get the best of both worlds, it makes little sense not to use extension methods :).

Cheers,
Mike