IEnumerable is the interface which provides basic iteration over a collection of some form. Most of the standard Generic collections support IEnumerable, as do all forms of array.
So by declaring a method parameter as taking an IEnumerable<int> you are saying that the method will accept anything which is a collection of int values. So you can call it with:
List<int> list = new List<int>();
list.Add(33);
list.Add(666);
Display(list, "header");
int[] array = {1, 2, 3, 4, 5, 6};
Display(array, "header");
Stack<int> stack = new Stack<int>();
stack.Push(1024);
stack.Push(2048);
Display(stack, "header");
...
public static void Display(IEnumerable<int> result, string header)
{
Console.WriteLine("{0}", header);
foreach (var element in result)
Console.Write("{0}", element);
}
Only classes that support IEnumerable can be used in a
foreach
loop. But the loop doesn't care what kind of IEnumerable you pass it, so your code will work regardless of the structure that it is given at run time.
You can use it as an interface to your own collections, provided that you implement the GetEnumerator method to provide access to the collection of objects.
MSDN has a lot more info on this - have a look there, and give it a try!