RangeExtensions 1.2.2
.NET 6.0
This package targets .NET 6.0. The package is compatible with this framework or higher.
.NET Core 3.1
This package targets .NET Core 3.1. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package RangeExtensions --version 1.2.2
NuGet\Install-Package RangeExtensions -Version 1.2.2
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="RangeExtensions" Version="1.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RangeExtensions --version 1.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: RangeExtensions, 1.2.2"
#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 RangeExtensions as a Cake Addin #addin nuget:?package=RangeExtensions&version=1.2.2 // Install RangeExtensions as a Cake Tool #tool nuget:?package=RangeExtensions&version=1.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
RangeExtensions
This package allows you to use 0..100
in foreach
expressions and implements RangeEnumerable
that supports a variety of LINQ-like operations as well as ICollection<int>
and IEnumerable<int>
.
- Correctness is verified against standard
IEnumerable<int>
andEnumerable.Range
behavior; - Performance is hand tuned to produce efficient native code with no allocations as long as
RangeEnumerable
isn't boxed (same applies to enumerator). However, even when boxed it is still faster thanEnumerable.Range
.
Features
Range enumeration
foreach (var i in ..100) // you can write 0..Length as just ..Length
{
Console.WriteLine(i);
}
Reverse range enumeration
for (var i = 100 - 1; i >= 0; i--)
{
Console.WriteLine(i);
}
// Can be written as
foreach (var i in 100..0)
{
Console.WriteLine(i);
}
Collecting to array or list
var numbers = (0..100).ToArray();
IEnumerable<int>
(some methods have bespoke implementations for performance)
var enumerable = (..100).AsEnumerable();
var sum = enumerable.Sum();
var count = enumerable.Count();
var average = enumerable.Average();
var firstTen = enumerable.Take(10);
var reversed = enumerable.Reverse();
// and others.
Performance
In short: 10x fast vs Enumerable.Range()
and as fast as a plain for
loop (there's small fixed overhead to check range correctness).
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
AMD Ryzen 7 5800X, 1 CPU, 16 logical and 8 physical cores
.NET SDK=7.0.100-rc.1.22363.32
[Host] : .NET 7.0.0 (7.0.22.36203), X64 RyuJIT
ShortRun : .NET 7.0.0 (7.0.22.36203), X64 RyuJIT
Job=ShortRun IterationCount=3 LaunchCount=1
WarmupCount=3
Method | Length | Mean | Error | StdDev | Ratio | RatioSD | Code Size | Gen 0 | Allocated |
---|---|---|---|---|---|---|---|---|---|
For | 100 | 23.94 ns | 0.556 ns | 0.030 ns | 1.00 | 0.00 | 20 B | - | - |
Range | 100 | 24.91 ns | 0.138 ns | 0.008 ns | 1.04 | 0.00 | 65 B | - | - |
RangeReverse | 100 | 27.40 ns | 0.245 ns | 0.013 ns | 1.14 | 0.00 | 65 B | - | - |
Enumerable.Range | 100 | 269.46 ns | 52.032 ns | 2.852 ns | 11.25 | 0.13 | 322 B | 0.0024 | 40 B |
Range.AsEnumerable | 100 | 24.92 ns | 0.522 ns | 0.029 ns | 1.04 | 0.00 | 67 B | - | - |
For | 10000 | 2,085.24 ns | 300.295 ns | 16.460 ns | 1.00 | 0.00 | 20 B | - | - |
Range | 10000 | 2,085.39 ns | 308.278 ns | 16.898 ns | 1.00 | 0.00 | 65 B | - | - |
RangeReverse | 10000 | 2,078.58 ns | 81.149 ns | 4.448 ns | 1.00 | 0.01 | 65 B | - | - |
Enumerable.Range | 10000 | 27,364.70 ns | 616.148 ns | 33.773 ns | 13.12 | 0.11 | 322 B | - | 40 B |
Range.AsEnumerable | 10000 | 2,104.25 ns | 464.044 ns | 25.436 ns | 1.01 | 0.01 | 67 B | - | - |
For | 10000000 | 2,086,119.92 ns | 289,496.016 ns | 15,868.253 ns | 1.00 | 0.00 | 20 B | - | - |
Range | 10000000 | 2,086,358.07 ns | 335,673.174 ns | 18,399.379 ns | 1.00 | 0.02 | 65 B | - | - |
RangeReverse | 10000000 | 2,083,810.55 ns | 342,667.388 ns | 18,782.756 ns | 1.00 | 0.01 | 65 B | - | - |
Enumerable.Range | 10000000 | 27,263,256.25 ns | 396,121.214 ns | 21,712.740 ns | 13.07 | 0.09 | 322 B | - | - |
Range.AsEnumerable | 10000000 | 2,075,666.41 ns | 45,777.672 ns | 2,509.229 ns | 1.00 | 0.01 | 67 B | - | - |
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETCoreApp 3.1
- No dependencies.
-
.NETStandard 2.0
- IndexRange (>= 1.0.2)
-
.NETStandard 2.1
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on RangeExtensions:
Repository | Stars |
---|---|
U8String/U8String
[work-in-progress] Highly functional and performant UTF-8 string primitive for C#
|