最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - C# range operator, why does this not throw an exception? (int x = someIntArray[2..2]) - Stack Overflow

programmeradmin3浏览0评论
int[] array = { 10, 20, 30, 40, 50 };
int[] range = array[2..2]; // gives empty array
int[] range2 = array[2..3]; // gives array with one element - 30

I understand range operators somewhat, like the upper bound is exclusive, so [2..3] is asking for index[2] and [0..3] gives elements [0], [1], [2]. Using [0..3] as an example, it seems like its saying "give me elements [0] through [3-1]".

When you use [2..2], it would seem like it's saying [2] through [2-1], which should throw an exception.

I know I'm missing something, I just can't figure out what.

int[] array = { 10, 20, 30, 40, 50 };
int[] range = array[2..2]; // gives empty array
int[] range2 = array[2..3]; // gives array with one element - 30

I understand range operators somewhat, like the upper bound is exclusive, so [2..3] is asking for index[2] and [0..3] gives elements [0], [1], [2]. Using [0..3] as an example, it seems like its saying "give me elements [0] through [3-1]".

When you use [2..2], it would seem like it's saying [2] through [2-1], which should throw an exception.

I know I'm missing something, I just can't figure out what.

Share Improve this question asked 22 hours ago akTedakTed 2152 silver badges9 bronze badges 3
  • 1 There's nothing in the language to prevent you from creating an empty array. It's better to think of an array as a start and a length. You aren't copying from elements from 2 - (2-1), you are copying (length = end - start) elements from the start position. – Jeremy Lakeman Commented 18 hours ago
  • 1 You can drill down the implementation through SharpLab sharplab.io/… and then open the source of the methods from their Docs page, you'll see that eventually it will just return Array.Empty when the requested length is zero. – Martheen Commented 15 hours ago
  • @JeremyLakeman length = end - start makes sense, thanks! – akTed Commented 14 hours ago
Add a comment  | 

2 Answers 2

Reset to default 6

I believe that viewing .. operator as a kind of filter like how a WHERE clause filters a dataset can help understand the behavior here. When one requests arr[2..2], it basically means "give me all the elements starting from index 2 inclusive up to index 2 exclusive" which cannot be satisfied by any elements. Hence, the result is an empty range.

Thinking about how ^0 works may also help. A statement like arr[^0] throws an exception because such an index is out of bounds of the array. However, arr[0..^0] does not throw an exception. Although the upper index does not exist, since it's exclusive, it does not cause an exception. Yet, there are still elements that satisfy the filter.

You can find the answer on this page of Microsoft: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/ranges search on System.Range

The syntax is [startposition ... endposition (excluded)] So if you ask for [2..2] you ask for the items from position 2 till 2 (exclude position 2) - which gives you no results.

If you want to "include" the last item, you enter it as: [2..^2]

发布评论

评论列表(0)

  1. 暂无评论