[CSHARP-4077] Support Range (..) operator Created: 24/Feb/22  Updated: 24/Feb/22  Resolved: 24/Feb/22

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Unknown
Reporter: James Kovacs Assignee: James Kovacs
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

C#8 introduced support for ranges using ... For example:

var middle = array[1..^1];

The array bounds can also be computed:

var moreMiddle = array[start..(start+numItems)];

We would have to support new overloads for collection methods that take Range parameters.

More details on the syntax can be found here:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#range-operator-



 Comments   
Comment by James Kovacs [ 24/Feb/22 ]

Turns out the C# compiler does not permit the Index and Range operators in expression trees. Including x.SomeArray[^1] or x.SomeArray[1..^1] in a LINQ expression results in the following compiler errors. The same errors occur if you assign the expression to Expression<T>. Unfortunately this syntax is not permitted even using C# 10 and .NET 6.

error CS8790: An expression tree may not contain a pattern System.Index or System.Range indexer access
error CS8791: An expression tree may not contain a from-end index ('^') expression.

Generated at Wed Feb 07 21:47:09 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.