Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

negative indexing in c#

// negative indexing is supported only in C# 8.0 and above
var favoriteShows = new string[] { "Firefly", "Cowboy Bebop", "Samurai Champloo" };

            Console.WriteLine(favoriteShows[^1]); // "Samurai Champloo"
            Console.WriteLine(favoriteShows[^2]); // "Cowboy Bebop"

            Index idx = ^3; // You can declare an index as a variable
            Console.WriteLine(favoriteShows[idx]); // "Firefly"
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #negative #indexing
ADD COMMENT
Topic
Name
5+3 =