Blog Logo


This package enables the usage of `System.Range` in `foreach` expressions and provides extensions to integrate it with LINQ as a faster replacement to `Enumerable.Range`. Correctness is verified against `IEnumerable` and `Enumerable.Range` behavior. Implementation tries its best to make abstractions either zero-cost or reasonably close to that. For critical paths, performance is tuned to be allocation-free and on par with regular `for` loops. Features include range enumeration, reverse range enumeration, select and where, collecting to array or list, aggregate, and other LINQ specializations. In .NET 7, `foreach (var i in 0..Length)` has the same performance as `for` loop. Otherwise, `RangeExtensions` is 2 to >10 times faster than `Enumerable.Range`. Using DynamicPGO significantly improves the performance of both.