RangeForeach.Sources
0.2.0
See the version list below for details.
dotnet add package RangeForeach.Sources --version 0.2.0
NuGet\Install-Package RangeForeach.Sources -Version 0.2.0
<PackageReference Include="RangeForeach.Sources" Version="0.2.0" />
paket add RangeForeach.Sources --version 0.2.0
#r "nuget: RangeForeach.Sources, 0.2.0"
// Install RangeForeach.Sources as a Cake Addin #addin nuget:?package=RangeForeach.Sources&version=0.2.0 // Install RangeForeach.Sources as a Cake Tool #tool nuget:?package=RangeForeach.Sources&version=0.2.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.
Please add the PrivateAssets="all"
attribute to the PackageReference
to avoid dependency to this package:
<PackageReference Include="RangeForeach.Sources" Version="0.2.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
If STEPPED_RANGE
is defined:
<DefineConstants>STEPPED_RANGE</DefineConstants>
this syntax can also be used:
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...
}
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 |
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 | 262 | 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.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 |