Search code examples
c#performancegenericsgarbage-collection

Does List<T> create garbage in C# in foreach


Correct me if im wrong but while doing a foreach an IEnumerable<T> creates garbage no matter what T is. But I'm wondering if you have a List<T> where T is Entity. Then say there is a derived class in the list like Entity2D. Will it have to create a new enumerator for each derived class? Therefore creating garbage?

Also does having an interface let's say IEntity as T create garbage?


Solution

  • List<T>'s GetEnumerator method actually is quite efficient.

    When you loop through the elements of a List<T>, it calls GetEnumerator. This, in turn, generates an internal struct which holds a reference to the original list, an index, and a version ID to track for changes in the list.

    However, since a struct is being used, it's really not creating "garbage" that the GC will ever deal with.


    As for "create a new enumerator for each derived class" - .NET generics works differently than C++ templates. In .NET, the List<T> class (and it's internal Enumerator<T> struct) is defined one time, and usable for any T. When used, a generic type for that specific type of T is required, but this is only the type information for that newly created type, and quite small in general. This differs from C++ templates, for example, where each type used is created at compile time, and "built in" to the executable.

    In .NET, the executable specifies the definition for List<T>, not List<int>, List<Entity2D>, etc...