Open In App

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

Last Updated : 28 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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




// 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);
    }
}
}


Output:

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

Example 2:




// 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]);
    }
}
}


Output:

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


Similar Reads

Creating an Index From the Specified Index at the Start of a Collection 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 a start index with the help of the FromStart(Int32) Method() provided by the Index struct. This method returns an index from the start of the given collection
2 min read
C# | Adding the elements of the specified collection to the end of the List
List<T>.AddRange(IEnumerable<T>) Method is used to add the elements of the specified collection to the end of the List<T>. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid value for reference types and it also allows duplicate elements.
3 min read
C# | How to insert the elements of a collection into the List at the specified index
List<T>.InsertRange(Int32, IEnumerable<T>) Method is used to insert the elements of a collection into the List<T> at the specified index. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid value for reference types and it also allows dupli
3 min read
C# | Insert an element into Collection<T> at specified index
Collection<T>.Insert(Int32, T) method is used to insert an element into the Collection<T> at the specified index. Syntax: public void Insert (int index, T item); Parameters: index : The zero-based index at which item should be inserted. item : The object to insert. The value can be null for reference types. Exception: This method will g
3 min read
C# | Remove element at specified index of Collection<T>
Collection<T>.RemoveAt(Int32) is used to remove the element at the specified index of the Collection<T>. Syntax: public void RemoveAt (int index); Here, index is the zero-based index of the element to remove. Exception: This method will give ArgumentOutOfRangeException if the index is less than zero OR index is equal to or greater than
2 min read
C# | Searching the index of specified object in Collection<T>
Collection<T>.IndexOf(T) method is used to search for the specified object and returns the zero-based index of the first occurrence within the entire Collection<T>. Syntax: public int IndexOf (T item); Here, item is the object to locate in the List<T>. The value can be null for reference types. Return Value: This method returns th
2 min read
C# | Get or set the element at specified index in Collection<T>
Collection<T>.Item[Int32] property is used to get or set the element at the specified index. Syntax: public T this[int index] { get; set; } Here, index is the zero-based index of the element to get or set. Return Value: The element at the specified index. Exception: This method will give ArgumentOutOfRangeException if the index is less than z
3 min read
Finding the End Index of the Specified Range in C#
The Range Structure is introduced in C# 8.0. It represents a range that has a start and end indexes. You are allowed to find the end index of the given ranges with the help of End Property provided by the Range struct.Syntax: public property Index End { Index get(); }; Here, Index represents the end index.Example 1: C/C++ Code // C# program to illu
2 min read
C# | Check if SortedSet and a specified collection share common elements
SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet.Overlaps(IEnumerable) Method is used to check whether a SortedSet and a specified collection share common elements or not. Properties: In C#, SortedSet class can be used to store, remove or view elements.
2 min read
C# | Check if HashSet and the specified collection contain the same elements
Here's an example code that demonstrates how to use the Overlaps method to check if a HashSet and a specified collection share common elements in C#: C/C++ Code using System; using System.Collections.Generic; class Program { static void Main(string[] args) { // Create a HashSet and a List HashSet<int> set1 = new HashSet<int> { 1, 2, 3,
3 min read
Article Tags :