This is ... something ... that you can go over. It could be List or Array or (almost) everything that supports the foreach . This is for when you want to use an object with a foreach , but you donβt know exactly what type you are dealing with, be it an array, a list, or something else.
So, the first advantage: if your methods accept an IEnumerable rather than an array or list, they become more powerful because you can pass more different types of objects to them.
Now, what makes IEnumerable really stand out is the iterator blocks ( yield keyword in C #). Iterator blocks implement an IEnumerable interface such as List or Array, but they are very special because, unlike List or Array, they often only hold state for one item at a time. Therefore, if you want you to iterate over lines in a very large file, you can write an iterator block to handle file input. Then you will never have more than one line of the file in memory at a time, and if you finish the loop earlier (maybe it was a search and you found what you needed), you may not need to read the whole file. Or, if you read the results from a large SQL query, you can limit the use of your memory to one record.
Another feature is that this estimate is lazy, so if you do the hard work of evaluating an enumerable as you read from it, this work is not done until it asks. This is very useful because often (say, to search again) you find that you may not need to do this work at all.
You can think of IEnumerable as being a list on time.
Joel Coehoorn Jun 10 '10 at 13:37 2010-06-10 13:37
source share