My question is simple, which of these 2 code snippets is better?(quicker)
Foo foos = foosAndBars.OfType<Foo>().ToArray();
foreach(Foo foo in foos)
foreach(FooBarParent foo in foosAndBars)
Similar Questions: How does OfType() Work? and the question that is "almost" identical to this one: LINQ + Foreach vs Foreach + If
Best How To :
First, in terms of performance, in the first snippet you are iterating over ONLY Foos where as in the second instance you're iterating over everything and checking during the iteration if it is a Foo.
Second, in terms of readability, I would be a bit surprised if I found the second option, whereas the first is quite normal.
Finally, as Peter Duniho noted in the comments, you can save yourself the overhead of creating a new array altogether thanks to deferred execution.
foreach(Foo foo in foosAndBars.OfType<Foo>())