IEnumerables and IEnumerable<T>s are a good thing:
- They allow returning set of values with a minimum contract and behaviour promise. You may alter the underlying data structure later to virtually anything, because nobody was able to use Your retval in a way You didnt mention. For example if You used a List instead Your retval consumers may add to or remove items from it and become coupled to that retval type. See next too.
- They allow returning unchangeable “lists”. Did You ever hunted a bug where Your retval instance was containing values which wasnt created by Your method?
- They may be lazy. You shouldnt known that Your retval consumers how want to use Your data. You may have a resource eater mapping process to run on 1000s of items, but the consumer may only want the Firts() item!
- LINQ. Just the whole thing uses IEnumerable‘s features and is returning something of this type.
- Etc. There could be a lot of things.
So I tend to be using these as a retval in every place where the only thing I want to return multiple instances of something.
Now the 50cent question: will this test run green?
[TestMethod] public void MyTestMethod() { IEnumerable<MyClass> result = GetMyClassList(); Assert.AreSame(result.First(), result.First()); }
Yes? Are You sure? Sure! The first item of an IEnumerable will always be the same!
Or not?
Lets see GetMyClassList‘s implementation:
public IEnumerable<MyClass> GetMyClassList() { IEnumerable<MyClass> ret = new MyClass[] { new MyClass(1) }; return ret; }
Yes, in case of this implementation the test becomes green.
But how about this:
public IEnumerable<MyClass> GetMyClassList() { IEnumerable<MyClass> ret = null; var someSourceList = new int[] { 1 }; ret = someSourceList.Select(i => new MyClass(i)); return ret; }
Now the test became red!
Why?
Because IEnumerable promises only a sequential access to items.
To items they contain.
In the first case these are items of a fixed array.
But in the second case the items are the values returned by a LINQ projection, which contains a mapping body.
When I call First() twice the IEnumerable‘s enumerator can only be recreated or Reset() and start over the evaluation. So the new MyClass(i) will run again and again resulting in different instances and failing test. And the resource friendly lazy evaluation may become shortly really bad too…
There is nothing new in the above, but in my head the parts of the explanation didnt connect to each other before.
But wait a minute! Is this meaning that when I use an IEnumerable I should known about its creation method?!?! This would break the basics or OOP!
No, I shouldnt known anything about it, just remember: IEnumerable only promises a sequential access to items!
When I consume it in a way that dont requires Reset() or enumerator recreation I need no extra steps:
var firstItem = result.First();
But when the consumption method results in multi-enumeration I should “fix” its items via explicit enumeration, for example:
var fixedResult = result.ToArray();
That allows You to use IEnumerable in a way it was designed and saves some ugly moments of You valuable life. 🙂