views:

27

answers:

1

I'd like to take the following if possible and combine them:

public static T[] ForEach<T>(this T[] TArray, Action<T> doWhat)
 {
  foreach (var item in TArray)
  {
   doWhat(item);
  }
  return TArray;
 }

The above handles array, below handles lists

 public static IEnumerable<T> ForEach<T>(this IEnumerable<T> TList, Action<T> action)
 {
  foreach (var item in TList)
   action(item);
  return TList;
 }

Is there something in common that both inherit from or implement as an interface?

+4  A: 

Array inherits from IEnumerable.

Robert Harvey
In other words: you can already pass arrays to the 2nd version of the function.
Joel Coehoorn
I missed that because before I wrote the array accepting function, I had forgotten to import my namespace for this section of code.
Maslow