Making foreach on an IEnumerable allocation-free using reflection and dynamic methods The article explains that when using `foreach` on an `IEnumerable` variable instead of a concrete type like `List`, the C# compiler generates code that allocates heap memory for the enumerator, causing slower performance and memory allocation. This occurs because the compiler uses pattern matching to find a `GetEnumerator()` method; with a concrete type it can use a value-type enumerator (allocation-free), but with `IEnumerable` it must use the reference-type `IEnumerator` interface, which requires a heap allocation. The author describes a technique using reflection and dynamic methods to eliminate this allocation when iterating over `IEnumerable`. In this post I describe a technique for reducing the allocation associated with calling foreach on an IEnumerable