RangeForeach.Sources 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package RangeForeach.Sources --version 1.1.0                
NuGet\Install-Package RangeForeach.Sources -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RangeForeach.Sources" Version="1.1.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RangeForeach.Sources --version 1.1.0                
#r "nuget: RangeForeach.Sources, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RangeForeach.Sources as a Cake Addin
#addin nuget:?package=RangeForeach.Sources&version=1.1.0

// Install RangeForeach.Sources as a Cake Tool
#tool nuget:?package=RangeForeach.Sources&version=1.1.0                

C# Range-Foreach Syntax Extensions

Provides the range-foreach syntax for C# 9.0 or higher.

Supported frameworks:

  • .NET Framework >= 2.0
  • .NET Core >= 1.0
  • .NET Standard >= 1.0

NOTE: This is a source-only package and will not introduce any explicit dependencies to your project. Make sure that the PackageReference has the PrivateAssets node or attribute like this:


<PackageReference Include="RangeForeach.Sources" Version="1.1.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>


<PackageReference Include="RangeForeach.Sources" Version="1.1.0" PrivateAssets="all" />

Range-Foreach Syntax

Reference source files or the NuGet package to write foreach loops like this:

foreach (var index in 0..100)
{
    // loop body...
}

which is equivalent to the legacy for loop below:

for (int index = 0; index < 100; index++)
{
    // loop body...
}

TIPS: 0 can be omitted in range expressions, e.g. ..100 (equivalent to 0..100).

NOTE: Use ^ to represent negative numbers, e.g. ^100..0 (instead of -100..0).

Stepped Syntax

Use the Step method to write foreach loops like this:

foreach (var index in (99..^1).Step(-2))
{
    // loop body...
}

which is equivalent to the legacy for loop below:

for (int index = 99; index > -1; index -= 2)
{
    // loop body...
}

Dependency Type Polyfill

This syntax requires the System.Range type (and also the System.Index type). Considering that early frameworks do not provide this type, this project includes the polyfill source.

If a third party package that includes the System.Range type (such as IndexRange, etc.) is referenced, define the INDEX_RANGE constant in the project file to avoid duplicate definitions:

<DefineConstants>$(DefineConstants);INDEX_RANGE</DefineConstants>

Performance Benchmark

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22621
AMD Ryzen 7 5800H with Radeon Graphics, 1 CPU, 16 logical and 8 physical cores
.NET SDK=7.0.201
  [Host]     : .NET 6.0.14 (6.0.1423.7309), X64 RyuJIT
  DefaultJob : .NET 6.0.14 (6.0.1423.7309), X64 RyuJIT
Method LoopCount Mean StdDev Ratio Code Size
CounterFor 1 0.0447 ns 0.0505 ns ? 20 B
RangeForeach 1 0.5299 ns 0.0747 ns ? 51 B
SteppedRangeForeach 1 6.3169 ns 0.0534 ns ? 163 B
EnumerableRangeForeach 1 14.2896 ns 0.5954 ns ? 337 B
CounterFor 3 0.4906 ns 0.0216 ns 1.00 20 B
RangeForeach 3 0.7141 ns 0.0180 ns 1.46 51 B
SteppedRangeForeach 3 6.5815 ns 0.0166 ns 13.44 163 B
EnumerableRangeForeach 3 18.9336 ns 0.1653 ns 38.64 337 B
CounterFor 10 2.1532 ns 0.0203 ns 1.00 20 B
RangeForeach 10 2.3766 ns 0.0143 ns 1.10 51 B
SteppedRangeForeach 10 9.1535 ns 0.2358 ns 4.24 163 B
EnumerableRangeForeach 10 42.6260 ns 1.1307 ns 19.76 337 B
CounterFor 100 27.2106 ns 0.3413 ns 1.00 20 B
RangeForeach 100 28.1094 ns 0.3127 ns 1.03 51 B
SteppedRangeForeach 100 30.6008 ns 0.6084 ns 1.13 163 B
EnumerableRangeForeach 100 319.5471 ns 3.6090 ns 11.75 337 B
CounterFor 1000 235.5779 ns 2.0112 ns 1.00 20 B
RangeForeach 1000 238.2080 ns 0.7198 ns 1.01 51 B
SteppedRangeForeach 1000 244.0087 ns 1.4962 ns 1.04 163 B
EnumerableRangeForeach 1000 3,029.0070 ns 17.3052 ns 12.88 337 B
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.8 226 2/8/2024
1.2.5 242 1/12/2024
1.2.0 235 12/6/2023
1.1.0 287 4/4/2023
1.0.1 261 3/12/2023
1.0.0 264 3/12/2023
0.9.0 305 3/8/2023
0.8.5 254 3/7/2023
0.8.1 329 3/5/2023
0.8.0 304 3/5/2023 0.8.0 is deprecated because it has critical bugs.
0.7.0 359 1/19/2023
0.6.0 305 12/31/2022
0.5.0 446 9/27/2022
0.4.0 469 7/20/2022
0.2.0 473 7/19/2022
0.1.1 546 7/5/2022
0.1.0 439 7/3/2022