Creating an Index From the End of a Collection at a Specified Index Position in C#

The Index Structure is introduced in C# 8.0. It represents a type that can be used to index a collection or sequence and it can be started from the start or the end. You are allowed to create an end index with the help of the FromEnd(Int32) Method provided by the Index struct. This method returns an index from the end of the given collection or sequence at a specified position.

Syntax:

public static Index FromEnd(int value);

Example: 1

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// concept of the FromEnd() Method
using System;
  
namespace example {
  
class Program {
  
    // Main Method
    static void Main(string[] args)
    {
        // Creating end index
        // Using FromEnd() method
        var in1 = Index.FromEnd(2);
        var in2 = Index.FromEnd(1);
        var in3 = Index.FromEnd(0);
        var in4 = Index.FromEnd(6);
  
        // Display index value
        Console.WriteLine("Index position is : {0} ", in1);
        Console.WriteLine("Index position is : {0} ", in2);
        Console.WriteLine("Index position is : {0} ", in3);
        Console.WriteLine("Index position is : {0} ", in4);
    }
}
}

chevron_right


Output:

Index position is : ^2 
Index position is : ^1 
Index position is : ^0 
Index position is : ^6

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// concept of the FromEnd() method
using System;
  
namespace example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        // Creating and initializing an array
        string[] greetings = new string[] {"Hello", "Hola", "Namaste"
                                "Bonjour", "Ohayo", "Ahnyounghaseyo"};
  
        // Creating end index
        // Using FromEnd() method
        var index_1 = Index.FromEnd(3);
        var index_2 = Index.FromEnd(1);
        var index_3 = Index.FromEnd(4);
        var index_4 = Index.FromEnd(5);
  
        // Displaying index and their values
        Console.WriteLine("Index: {0} Value: {1}"
                     index_1, greetings[index_1]);
  
        Console.WriteLine("Index: {0} Value: {1}"
                     index_2, greetings[index_2]);
  
        Console.WriteLine("Index: {0} Value: {1}"
                     index_3, greetings[index_3]);
  
        Console.WriteLine("Index: {0} Value: {1}",
                     index_4, greetings[index_4]);
    }
}
}

chevron_right


Output:

Index: ^3 Value: Bonjour
Index: ^1 Value: Ahnyounghaseyo
Index: ^4 Value: Namaste
Index: ^5 Value: Hola



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.