Andrew Lock 1/20/2026

Making foreach on an IEnumerable allocation-free using reflection and dynamic methods

Read Original

This technical article details a method for making foreach loops on IEnumerable<T> allocation-free in C#. It explains the performance difference between iterating over a concrete List<T> and an IEnumerable<T>, demonstrates the issue with benchmarks, and introduces an advanced solution using reflection and dynamic methods to avoid boxing and interface dispatch overhead.

Making foreach on an IEnumerable allocation-free using reflection and dynamic methods

Comments

No comments yet

Be the first to share your thoughts!

Browser Extension

Get instant access to AllDevBlogs from your browser

Top of the Week