Clarification on ranges #3877
Unanswered
AartBluestoke
asked this question in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
can we please have a section in https://github.com/dotnet/csharplang/blob/master/proposals/csharp-8.0/ranges.md which points out the contrast between the two different meanings of the index from end operator, depending on if it is an isolated unary_expression, or part of a range_expression .
the System.Index section begins
this ^1 compiles to array3[new Index(1, true).GetOffset(array3.Length)];
the '^1' here means "from the length", a 0 indexed statement. The examples in this section should have a case demonstrating that ^0 is an error here.
This especially confusing when the documentation says
except that ^0 here is translated to the second parameter of the slice, and is a 1 indexed statement
The details are there, but at separate parts of the document.
the Runtime Behavior summary says
According to the spec,
what does [0..^0] mean?
so this means Array.Slice(0,(reciever.Length-0)-0); or "the entire array" as expected
what does [0..Array.Length] mean?
so this means Array.Slice(0,(reciever.Length-0)); or "the entire array" as expected...
tldr:
Basically the first element of a range is 0 indexed, the second is 1 indexed, and ^1 is viable in both. The documentation should clarify this with examples like
var theValue = array[^0]; // an error
var theValue = array[^1]; // the last element
var theValue = array[0..1]; // just the first element
var theValue = array[0..array.Length]; // all the elements
var theValue = array[0..^0]; // all the elements
var theValue = array[0..^1]; // all but the last element
Beta Was this translation helpful? Give feedback.
All reactions