Click here to Skip to main content
15,911,707 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone
I'm confused about IEnumerable. i don't know which it what's mean? and when can i use it as an interface?
I've faced with IEnumerable like this:

var sorted=
from value in values
orderby value
select value;

Diplay(sorted, "sorted:");

Public Static Void Display(IEnumerable <int>>; result, string header)
{
cosol.writeline ("{0}", header);
Foreach (var element in result)
cosole.write("{0}", element)
}
Posted

You didn't try to read the documentation[^], did you?

The documentation states: "[IEnumerable ] Exposes the enumerator, which supports a simple iteration over a non-generic collection." and provides a code sample (how to implement the IEnumerable interface for a custom collection).

[Update]
Simply stated, the IEnumerable interface allows you to use the (object implementing it in a) foreach loop.
That is, being a consumer of IEnumerable object is easy:
C#
List<string> li = new List<string>{ "foo", "boo", "goo" };

foreach (string s in li) // this is allowed because List<T> implements the IEnumerator interface
{
  Console.WriteLine(s);
}


On the other hand if you are writing your own collection class and wish allow the user to use it in a foreach loop then you have to implement the IEnumerator interface in your class itself (the MSDN sample shows how to accomplish this rather more complex task).
[/Update]
 
Share this answer
 
v3
Comments
Elham.Deljooei 8-Apr-13 6:41am    
no, i didn't do.
I've studied it into Ditel's book.
I've looked for about it alot, but i can't underestand.
Could you help me?
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:
C#
    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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900