I read a lot about why it is better to return an IEnumerable instead of an IList (or something like this). But now I stuck at some point.
Consider something like this: I have some functions that do multiple enumerations and therefore take an IReadOnlyCollection as a parameter, create a new List and return it as an IEnumerable.
IEnumerable<int> Foo1(IReadOnlyCollection<int> bar)
{
bar.Any();
bar.First();
return new List<int>() {1, 2, 3};
}
IEnumerable<int> Foo2(IReadOnlyCollection<int> bar)
{
bar.Any();
bar.First();
return new List<int>() {1, 2, 3};
}
IEnumerable Foo3 ...
Now if I want to call them one after another I have to do something like this
var tmp = Foo1(new List<int>() {1, 2, 3});
tmp = Foo2(tmp.ToList());
tmp = Foo3(tmp.ToList());
tmp = Foo4(tmp.ToList());
tmp = Foo5(tmp.ToList());
That means I have to create a 11 lists. The first one, one in every function and one for every function call. But in fact it would be enough to create 6 lists. Is there a better way or is this simply a possible drawback when returning IEnumerable?