Finding the Index which Points Beyond the Last Element 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 find the index, which points beyond the last element of the specified collection with the help of End Property provided by the Index struct.

Syntax:

public static property Index End { Index get(); };

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how 
// to get the End index
using System;
  
namespace example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
  
        // Creating new indexes
        // Using Index() constructor
        var in1 = new Index(1, true);
        var in2 = new Index(3, false);
  
        // Getting the end index
        var res1 = Index.End;
  
        // Displaying the index
        Console.WriteLine("Index: {0}", in1);
        Console.WriteLine("Index: {0}", in2);
        Console.WriteLine("End Index: {0}", res1);
    }
}
}

chevron_right


Output:

Index: ^1
Index: 3
End Index: ^0

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// concept of the End index
using System;
  
namespace example {
  
class GFG {
  
     // Main Method
    static void Main(string[] args)
    {
        string[] greetings = new string[] {"Hello", "Hola", "Namaste",
                                "Bonjour", "Ohayo", "Ahnyounghaseyo"};
  
        // Get the end index
        var res = Index.End;
  
        // Checking the given index
        // is the end index or not
        if (res.Equals (^0) == true) {
  
            Console.WriteLine("The given index is "+
                        "beyond the last element");
        }
  
        else {
  
            Console.WriteLine("The given index is not"+
                          " beyond the last element");
        }
    }
}
}

chevron_right


Output:

The given index is beyond the last element



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.