+ 3

What's "IEnumerable<T>" function in C#?

I know that "IEnumerable<T>" is a generic type, but I'm not understanding its function on C#. Can someone explain me?

1st Jul 2018, 1:07 AM
Artur Azevedo
Artur Azevedo - avatar
2 Answers
+ 1
A List is usually used for looping through it, but you can do more things with Lists, like add things to them, or pick out a single element in the middle of a list like `myList[10]`. IEnumerables are things you can only loop through ("enumerate"), nothing else. All Lists are IEnumerable, but not all IEnumerables are Lists. For example you could write an IEnumerable that generates all prime numbers; it doesn't fit in a List because the list would be infinite, but you could generate one after the other no problem. You can step through any IEnumerable with a for loop. foreach(var item in myIEnumerable){ ... } IEnumerables are also used in LINQ if you are familiar with that.
1st Jul 2018, 2:38 AM
Schindlabua
Schindlabua - avatar
+ 1
Hey! Here is an example. Hopefully it's painfully clear. https://code.sololearn.com/cbT1sok2tlcl/?ref=app
1st Feb 2019, 2:52 PM
Cameron Hansen
Cameron Hansen - avatar