Code Monkey home page Code Monkey logo

indexrange's Introduction

IndexRange

This repository provides implementations (copied from corefx) 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 in projects that target .NET Framework or netstandard2.0.

Installing

NuGet Pre Release

The package is available on NuGet. To install, run:

dotnet add package IndexRange

Build Status

Build Status

Using Range with Arrays

The C# compiler needs the RuntimeHelpers.GetSubArray<T> 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<T>

A workaround is to add a reference to System.Memory and use Span<T>. 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:

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<T>

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.

indexrange's People

Contributors

bgrainger avatar davidhunter22 avatar baka632 avatar

Stargazers

Funny_Silkie avatar Mohammad Javad Ebrahimi avatar Banncan avatar sunguangdong avatar Valentin Arthur Thomas avatar Nickc01 avatar  avatar pedoc avatar Sam Segers avatar n0099 avatar Mykola Musiienko avatar SamTi(三台) avatar Media Explorer avatar Ahmed Walid avatar  avatar k1mlka avatar  avatar eric0210 avatar kawhi avatar vzeng avatar AlexLEWIS avatar well.james avatar jian玄冰 avatar Jonathan Peel avatar  avatar RoyLai avatar 巨应君 avatar Galli Davide Francesco Maria avatar Ulysses avatar MysticBoy avatar  avatar Eduard de Jong avatar  avatar bopin avatar Maximilian Haru Raditya avatar Protiguous avatar  avatar  avatar Feng Chang avatar RyanYANG avatar Sedat Kapanoğlu avatar  avatar Soichiro Sugimoto avatar Mikael Dúi Bolinder avatar Chris Motch avatar  avatar Damian Viktorovich Morozov avatar Hjalmar avatar  avatar redbeardt avatar Cédric Luthi avatar ouboxu avatar Minhyuk Kim avatar Fabien Ménager avatar  avatar Dmitrii Filippov avatar Harry Esau avatar Enes Sadık Özbek avatar S. Tarık Çetin avatar Jesse Talavera avatar Niccolò Maltoni avatar Tim avatar Kyle Wascher avatar Tom Pažourek avatar  avatar Bruno Logerfo avatar Viktor Svub avatar  avatar Ben Hutchison avatar Bruno Garcia avatar Stephen Klancher avatar Josh DeGraw avatar Fabrício Godoy avatar Johnny Santos avatar Stuart Lang avatar  avatar Ajay Marathe avatar 王宇逸 avatar Ed Ball avatar

Watchers

 avatar James Cloos avatar Stuart Lang avatar  avatar  avatar

indexrange's Issues

Upgrade nuget package references

Upgrade the packages referenced to the latest compatible versions.
Note xunit.runner.visualstudio can't move to the latest version as it no longer supports older frameworks versions

Can't use using

Added the nuget
But I can't add using IndexRange;
Whatelse should I do beside adding the nuget

Support .NET Framework 3.5

It would be useful to support .NET Framework 3.5 as it is a Long Term Support version that does not go out of support for a few years.

I gave this a try and it was trivial to do. The main issue was the lack of support for ValueTuple so I added a conditional package reference to the package ValueTupleBridge which back ports ValueTuple support

Support UWP (min version < 16299)

Though we can use IndexRange in UWP that minimum version is 16299 or later, former version of UWP can't use this library because they can't use .NET Standard 2.0.

For developers who still want to create apps on former version of Windows (like Windows 10 Mobile), it would be useful to support uap10.0.

The main issue was the lack of support for targeting uap10.0, so I used MSBuildSdkExtras which allows us to target uap10.0.

I've completed a draft version in here.

Add new package IndexRange.SliceSupport

Hi @bgrainger, have been checking this out and I like it, I won't bother releasing my competing package as this approach looks great.

My only suggestion is to add a secondary package, something like IndexRange.SliceSupport, which would add RuntimeHelpers code as source into consuming projects (naming is hard). We'd recommend they'd use PrivateAssets="All", so that the dependency is local to that project. I haven't quite figured out if this needs to be split into 2, one for array slicing and one for the ones you get from System.Memory to avoid clashing.

What do you think to this? If you are happy with this then I can make a start on a PR. 😃

Add `CLSCompliantAttribute`

Like strong naming, this is generally pointless these days, but it's viral. If you have a project that declares CLS compliance, you get compiler warnings on any types exposed in public APIs that are not also declared CLS compliant.

All the BCL assemblies are marked CLS compliant, so that's a difference between this package and what would be the 'official' one if it existed.

[assembly: CLSCompliant(true)]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.