0

Reading an answer I found the following instruction : input[..^3]

I found that ^ change the orientation of the index. From end.

Doing a basic test it seams to be a feature of .net5

Console.WriteLine("Hello World"[^3]);    // The 3rd char starting from the end
//Console.WriteLine("Hello World"[.^3]); // Error Identifier expected, invalid syntax
Console.WriteLine("Hello World"[..^3]);  // Everything except the last 3, like a substring

https://dotnetfiddle.net/elDmzh

And mean "Everything except the last 3, like a substring", but that didn't gave me much result in the MSDN.
What is this indexer name? Is it a SubString syntactic sugar?

Self
  • 349
  • 1
  • 8
  • 3
    This is called the range operation – nbokmans Mar 02 '21 at 09:08
  • 4
    [Ranges](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/ranges) – Izzy Mar 02 '21 at 09:09
  • 1
    arg this gets asked 3 times a day – TheGeneral Mar 02 '21 at 09:12
  • 1
    Yes! Knowing the name is the key. As ".." or "dotdot operator" are not the wisest keyword for a research. – Self Mar 02 '21 at 09:12
  • @TheGeneral, get the man(me) a duplicate link. I didn't get any thing from my search. And I understand why there is so mutch question about single operator like `?` and now `..`. – Self Mar 02 '21 at 09:14
  • 2
    For strings, the syntax will call Substring. For Spans it will essentially call Slice. For Arrays it will create a new array consisting of the sub array defined by the range. Other types can accept Index and/or Range arguments and do what makes sense. For example List only accepts Index and not Range – pinkfloydx33 Mar 02 '21 at 09:16

0 Answers0