Index and Range should accept negative values, as well. #84531
-
The struct System.Index doesn't currently accept negative indexes. I am quite sure there are a lot of custom collections out there that support negative indexes, but they cannot use the struct System.Index. Negative indexes would also open the door for negative ranges and that would be a good thing, as well. A range like -7..-3 makes sense for a collection that supports negative indexes. I may be wrong, but it seems the restriction of Index to positive integer values is dictated only by the current implementation. The author of the struct Index chose to use the negative side of the internal Int32 field to store "from the end" indexes, so it cannot store a negative value that comes from the application. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
Moving to runtime. This is an api, not a language, decision. Thanks! |
Beta Was this translation helpful? Give feedback.
-
From implementation sight of view, |
Beta Was this translation helpful? Give feedback.
-
Can you share examples? |
Beta Was this translation helpful? Give feedback.
-
Stephen, Thank you for your attention. The API/collection I am working on doesn't need negative indexes. However, it wants to support selecting data based on "directional ranges"(ascending & descending). A "descending range" is something like 9..5 and the API provides the result starting from the index 9 and then all the other values down to 5. I chose to present this "issue" in a general context of collections that need or maybe prefer to support negative indexes. Why I chose that? Because I am convinced there are such collections. You asked me for an example. Any list of data that can have a kind of "symmetry" with respect to a specific point can be an example, such as a list of temperatures. |
Beta Was this translation helpful? Give feedback.
-
Thank you all guys for your support. It was nice talking to you. |
Beta Was this translation helpful? Give feedback.
Index
is internally a singleint
field today. It supports indices from the start of the collection which are internally represented as0
to+2147483647
, inclusive. It then supports indices from the end of the collection which are internally represented as-1
to-2147483648
, inclusive.Thus, the bits that would be used to represent negative indices are "already taken" in order to efficiently support "from end of the collection". In order t…