A lot of the developers I work with feel more comfortable working with a List
as opposed to IEnumerable
(for example). I am wondering whether there is any performance impact for ToList()
overuse. For example, or, will use ToList()
after ordering to get a list back out again i.e.
private void ListThinger(List<T> input)
{
input = input.OrderBy(s => s.Thing).ToList();
foreach(var thing in input)
{
// do things
}
}
My question is:
- How efficient is the
ToList()
method? Will it create a new list and how much memory does that take, assuming the contents are POCOs? Does this change if its a value type rather than a POCO? - Will the size of the list determine efficiency or does size of list not determine cost of
ToList()
? - If a list is cast to an
IEnumerable
and thenToList()
is called on it, will it just return the original object?
P.s. I understand that a single use of ToList won't break any backs, but we are building a highly concurrent system that is currently CPU bound so I am looking for little wins that, when scaled, will add up to a big improvement