What is required for indexing with ranges? [Answered]
What exactly is required for indexing with a range (ex.
obj[..3]
) to be supported on a class? Just having an indexer public T this[int index]
causes a type error because you can't convert between a range and an int. I've know Slice
exists and supposedly has something to do with range indexing, but I can't get it to work by just having a public int Slice(int index, int length)
method.7 Replies
Ranges and indices - C# 8.0 draft specifications
This feature specification describes the syntax for ranges and indices, which support indexing individual elements of a sequence or a range of a sequence from the start or end of that sequence.
Okay so I have this example code, although it seems that it's calling
Slice
instead of doing anything with the indexer? Commenting out the Slice
method causes a type error.Ohhh that's only for
Index
, as in [^1]
lol i didn't read enough of the article
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/ranges#implicit-range-support
Ranges and indices - C# 8.0 draft specifications
This feature specification describes the syntax for ranges and indices, which support indexing individual elements of a sequence or a range of a sequence from the start or end of that sequence.
Okay so
Index
(ex. [^1]
) only requires a int Count
or int Length
property plus a T this[int]
property, while Range
(ex. [..3]
) requires a int Count
or int Length
property plus a T Slice(int, int)
method.✅ This post has been marked as answered!