Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bgrainger/IndexRange
Implementations of System.Index and System.Range for netstandard2.0 and .NET Framework.
https://github.com/bgrainger/IndexRange
Last synced: 7 days ago
JSON representation
Implementations of System.Index and System.Range for netstandard2.0 and .NET Framework.
- Host: GitHub
- URL: https://github.com/bgrainger/IndexRange
- Owner: bgrainger
- License: mit
- Created: 2019-10-02T22:06:55.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-08-22T14:13:08.000Z (about 1 year ago)
- Last Synced: 2024-10-27T11:13:37.856Z (17 days ago)
- Language: C#
- Homepage: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#indices-and-ranges
- Size: 64.5 KB
- Stars: 80
- Watchers: 5
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# IndexRange
This repository provides implementations (copied [from corefx](https://github.com/dotnet/corefx/tree/d152d19f0be3dcea1a32f452e9d9940e990574d7/src/Common/src/CoreLib/System))
of `System.Index` and `System.Range` for `netstandard2.0` and .NET Framework.This lets you use the new C# 8.0 [index and range features](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#indices-and-ranges) in projects that target
.NET Framework or `netstandard2.0`.## Installing
[![NuGet Pre Release](https://img.shields.io/nuget/v/IndexRange.svg)](https://www.nuget.org/packages/IndexRange/)
The package is available [on NuGet](https://www.nuget.org/packages/IndexRange). To install, run:
```
dotnet add package IndexRange
```## Build Status
[![Build Status](https://dev.azure.com/bgrainger/Public/_apis/build/status/bgrainger.IndexRange?branchName=master)](https://dev.azure.com/bgrainger/Public/_build/latest?definitionId=3&branchName=master)
## Using Range with Arrays
The C# compiler needs the `RuntimeHelpers.GetSubArray` method to be available to create subranges from arrays. This method is only available in `netstandard2.1`
and .NET Core 3.0, so creating subranges from arrays will fail to compile in .NET Framework.### Use Span\
A workaround is to add a reference to [System.Memory](https://www.nuget.org/packages/System.Memory/) and use `Span`. Not only does this compile, it's much more
efficient as it doesn't create a new array and copy the sliced data to it:```csharp
int[] array = new[] { 1, 2, 3, 4, 5, 6 };// don't do this:
// var slice = array[1..^1];// do this:
var slice = array.AsSpan()[1..^1];
```### Define GetSubArray\
The other fix is to define the necessary method in your source code. Copy the following code into your project:
https://gist.github.com/bgrainger/fb2c18659c2cdfce494c82a8c4803360
That type is not in this NuGet package so that the C# compiler doesn't warn that `The predefined type 'RuntimeHelpers' is defined in multiple assemblies`.