I’ve been moving to using more of the range based code and shorthand in my C# code recently- The ranges are pretty cool in how you can specify easily the lower..upper shorthand to specify a subset of an array or even letters in a string.
Recently I fully noticed something odd about the upper bound- if you perform this-
string s = "this_is_a_string";
string s2 = s[0..5];
One might assume this would pull the characters starting at index 0 (or “t”) and continue through the character at index 5 (“i”)- but instead the string will be:
"this_"
What?
So it turns out, the .Net team had to make a decision as to whether the upper bound of ranges would be inclusive or exclusive of the last element – as discussed here. The most obvious answer would be inclusive, since the letter at index 5 should be “i”… but they apparently weighted the pros and cons (and, there are quite a few things to consider), and decided on exclusive.
This results in a cleaner implementation for most use cases (as demonstated in the linked document above), but you definitely need to know this information when working with range notation. Feels like it needs a big flashing warning sign on it somewhere!
My last comment, I find it curious that a second range notation was being considered for start:length, instead of just start..end. This would be quite useful as well but we’ll have to see if it gets implemented in the future (unless it has been but I’m just overlooking it somehow).