RangeForeach.Sources 0.5.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package RangeForeach.Sources --version 0.5.0                
NuGet\Install-Package RangeForeach.Sources -Version 0.5.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="0.5.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 0.5.0                
#r "nuget: RangeForeach.Sources, 0.5.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=0.5.0

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

C# Range-Foreach Syntax Extensions

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

Supported frameworks:

  • .NET Framework >= 4.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. Add the PrivateAssets="all" attribute to the PackageReference to avoid dependency to this package:

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

Range-Foreach Syntax

Reference this 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...
}

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 >= 0; 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 package 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>INDEX_RANGE</DefineConstants>

Performance Benchmark

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
AMD Ryzen 7 5800H with Radeon Graphics, 1 CPU, 16 logical and 8 physical cores
.NET SDK=6.0.302
  [Host]     : .NET 6.0.7 (6.0.722.32202), X64 RyuJIT
  DefaultJob : .NET 6.0.7 (6.0.722.32202), X64 RyuJIT
Method LoopCount Mean StdDev Ratio RatioSD
LegacyForLoop 1 0.0237 ns 0.0096 ns 1.00 0.00
RangeForeachLoop 1 0.2197 ns 0.0056 ns 10.54 3.87
SteppedRangeForeachLoop 1 6.5912 ns 0.0174 ns 317.22 118.54
EnumerableRangeForeachLoop 1 13.6163 ns 0.1818 ns 653.25 246.38
LegacyForLoop 3 0.4609 ns 0.0097 ns 1.00 0.00
RangeForeachLoop 3 0.6935 ns 0.0064 ns 1.50 0.03
SteppedRangeForeachLoop 3 6.7897 ns 0.0347 ns 14.74 0.28
EnumerableRangeForeachLoop 3 19.3017 ns 0.1484 ns 41.84 0.90
LegacyForLoop 10 2.1043 ns 0.0124 ns 1.00 0.00
RangeForeachLoop 10 2.5624 ns 0.0194 ns 1.22 0.01
SteppedRangeForeachLoop 10 8.2221 ns 0.0250 ns 3.91 0.02
EnumerableRangeForeachLoop 10 39.3786 ns 0.2443 ns 18.71 0.16
LegacyForLoop 100 26.4636 ns 0.0874 ns 1.00 0.00
RangeForeachLoop 100 27.4956 ns 0.0970 ns 1.04 0.01
SteppedRangeForeachLoop 100 31.3107 ns 1.0000 ns 1.20 0.04
EnumerableRangeForeachLoop 100 292.9096 ns 1.0386 ns 11.06 0.04
LegacyForLoop 1000 230.1425 ns 0.2999 ns 1.00 0.00
RangeForeachLoop 1000 234.6219 ns 0.3923 ns 1.02 0.00
SteppedRangeForeachLoop 1000 246.3007 ns 3.6063 ns 1.07 0.02
EnumerableRangeForeachLoop 1000 2,780.5681 ns 5.4667 ns 12.08 0.02
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 222 2/8/2024
1.2.5 240 1/12/2024
1.2.0 231 12/6/2023
1.1.0 285 4/4/2023
1.0.1 259 3/12/2023
1.0.0 260 3/12/2023
0.9.0 303 3/8/2023
0.8.5 252 3/7/2023
0.8.1 327 3/5/2023
0.8.0 302 3/5/2023 0.8.0 is deprecated because it has critical bugs.
0.7.0 357 1/19/2023
0.6.0 303 12/31/2022
0.5.0 444 9/27/2022
0.4.0 467 7/20/2022
0.2.0 471 7/19/2022
0.1.1 544 7/5/2022
0.1.0 437 7/3/2022