Foreach can cause allocations, but at least in newer versions .NET and Mono, it doesn't if you're dealing with the concrete System.Collections.Generic
types or arrays. Older versions of these compilers (such as the version of Mono used by Unity3D until 5.5) always generate allocations.
The C# compiler uses duck typing to look for a GetEnumerator()
method and uses that if possible. Most GetEnumerator()
methods on System.Collection.Generic
types have GetEnumerator() methods that return structs, and arrays are handled specially. If your GetEnumerator()
method doesn't allocate, you can usually avoid allocations.
However, you will always get an allocation if you are dealing with one of the interfaces IEnumerable
, IEnumerable<T>
, IList
or IList<T>
. Even if your implementing class returns a struct, the struct will be boxed and cast to IEnumerator
or IEnumerator<T>
, which requires an allocation.
NOTE: Since Unity 5.5 updated to C# 6, I know of no current compiler release that still has this second allocation.
There's a second allocation that is a little more complicated to understand. Take this foreach loop:
List<int> valueList = new List<int>() { 1, 2 };
foreach (int value in valueList) {
// do something with value
}
Up until C# 5.0, it expands to something like this (with certain small differences):
List<int>.Enumerator enumerator = valueList.GetEnumerator();
try {
while (enumerator.MoveNext()) {
int value = enumerator.Current;
// do something with value
}
}
finally {
IDisposable disposable = enumerator as System.IDisposable;
if (disposable != null) disposable.Dispose();
}
While List<int>.Enumerator
is a struct, and doesn't need to be allocated on the heap, the cast enumerator as System.IDisposable
boxes the struct, which is an allocation. The spec changed with C# 5.0, forbidding the allocation, but .NET broke the spec and optimized the allocation away earlier.
These allocations are extremely minor. Note that an allocation is very different from a memory leak, and with the garbage collection, you generally don't have to worry about it. However, there are some scenarios when you do care about even these allocations. I do Unity3D work and until 5.5, we couldn't have any allocations in operations that happen every game frame because when the garbage collector runs, you get a noticeable lurch.
Note that foreach loops on arrays are handled specially and don't have to call Dispose. So as far as I can tell, foreach has never allocated when looping over arrays.