4
int []arr = new int[4];
arr[^1];   // returns the last element

I'm trying to figure out the syntax above. It's returning the last element, but why?

atiyar
  • 7,762
  • 6
  • 34
  • 75
programmer
  • 95
  • 2
  • 7
  • [Research ranges](https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/ranges-indexes) – TheGeneral Mar 01 '21 at 06:53
  • 2
    See [this answer](https://stackoverflow.com/a/55498674) in duplicate. And, of course, [the documentation](https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/ranges-indexes), which is where you should always look before asking the worldwide community to teach you the syntax of a well-documented programming language. – Peter Duniho Mar 01 '21 at 06:55
  • See also closely related https://stackoverflow.com/questions/54092458/why-doesnt-the-new-hat-operator-index-from-the-c-sharp-8-array-slicing-feature and https://stackoverflow.com/questions/14323994/c-sharp-array-slice-without-copy – Peter Duniho Mar 01 '21 at 06:57
  • @PeterDuniho OP is facing the symbol `^` in their code, and none of the links you mentioned include `^` in their title. You have to know that C# has something called `Index` first, only then you can find about `[^1]`. Try googling "c# array ^1" and see if you can find the documentation or any of your links. – atiyar Mar 01 '21 at 07:16
  • 1
    @atiyar: I found those links with the Google search https://www.google.com/search?q=c%23+array+%5E+syntax+span+site:stackoverflow.com. So, no...it's not hard at all to find them. Regardless, they _do_ answer the question, and the OP did _not_ present any evidence of having made _any_ due diligence before posting their question. – Peter Duniho Mar 01 '21 at 07:20
  • @PeterDuniho Yes I see your google search, but I see neither the documentation in the result, nor the `^` until the 14th entry. Also, even if the linked posts _do_ answer the questions, how would one know that those are the posts they are looking for in the first place? Remember, I'm not looking for array slicing, or some hat operator or newly added feature in C#. I'm looking for `arr[^1]` which I just found in my code. – atiyar Mar 01 '21 at 07:43
  • 1
    @atiyar: _"I see neither the documentation in the result"_ -- the search is restricted to Stack Overflow. And Stack Overflow isn't a documentation site. So, duh...of course the _documentation_ doesn't show up in that search. Which is, by the way, **the point**. Frankly, no question on Stack Overflow should ever take the form of "what does X mean in language Y". That's what the language documentation is for. Showing the author of the question posts that do answer what they asked is being generous. Such questions _ought_ to just be deleted with prejudice. – Peter Duniho Mar 01 '21 at 07:48
  • @PeterDuniho "_That's what the language documentation is for_" - I agree, but try finding the language documentation yourself with something like "c# array index ^ syntax" or something. And I know your search is restricted to Stack Overflow. You presented your search as an example, that's why I mentioned about its result. – atiyar Mar 01 '21 at 07:59
  • @atiyar: Google "C# language reference". _First_ hit from Google has a menu of pages under the main hit. Click "Operators". Ctrl+F, enter `^` in the search box. Click on the _first_ hit from that search. That takes you to, **gasp!**: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#index-from-end-operator-. It is _not_ that hard to look stuff up in the language documentation. – Peter Duniho Mar 01 '21 at 17:21

1 Answers1

3

C# 8.0 and going forward, declared the new ranges and indexes

Among them the ^ operator:

Let's start with the rules for indices. Consider an array sequence. The 0 index is the same as sequence[0]. The ^0 index is the same as sequence[sequence.Length].

So it a way to search an indexable object in reverse, without needing to iterate like sequence[sequence.Length - i].

string[] words = new string[]
{
                // index from start    index from end
    "The",      // 0                   ^9
    "quick",    // 1                   ^8
    "brown",    // 2                   ^7
    "fox",      // 3                   ^6
    "jumped",   // 4                   ^5
    "over",     // 5                   ^4
    "the",      // 6                   ^3
    "lazy",     // 7                   ^2
    "dog"       // 8                   ^1
};              // 9 (or words.Length) ^0
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61