Reputation: 229
I'm creating a few extension methods that i want to use on different collections mostly those that implement IList
and array collections []
whenever im creating an extension method i need to create 2 instead of just 1 so that it can work for both
public static IList<T> Shuffle<T>(this IList<T> inputCollection)
{
}
public static T[] Shuffle<T>(this T[] inputCollection)
{
}
Most of my methods require work with the indexes so i cant use IEnumerable
is there some other way that i can write just 1 extension method and use it for both of the desired collections ? Also sometimes i use the string
which is one more method.
Upvotes: 2
Views: 310
Reputation: 1
I would suggest using this extension method, because all List
objects are inherited from IEnumerable
.
public static IEnumerable<T> MethodName<T>(this IEnumerable<T> value)
Upvotes: 0
Reputation: 186668
You can try
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> value)
signature; and use it
IList<T> list = Shuffle(myList).ToList();
T[] array = Shuffle(myArray).ToArray();
or even
IList<T> list = myList.Shuffle().ToList();
T[] array = myArray.Shuffle().ToArray();
Upvotes: 6