Open In App

Finding the Index of First Element of the Specified Sequence 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 find the index, which points the first element of the specified collection or sequence with the help of Start Property provided by the Index struct.

Syntax:

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

Example 1:




// C# program to illustrate the
// concept of the start 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);
  
        // Get the start index
        var res1 = Index.Start;
  
        // Displaying the index
        Console.WriteLine("Index: {0}", in1);
        Console.WriteLine("Index: {0}", in2);
        Console.WriteLine("Start Index: {0}", res1);
    }
}
}


Output:

Index: ^1
Index: 3
Start Index: 0

Example 2:




// C# program to illustrate the
// concept of the start index
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"};
  
        // Get the end index
        var res = Index.Start;
  
        // Checking the given index 
        // is the start index or not
        if (res.Equals(0) == true) {
  
            Console.WriteLine("The given index is start index"+
                      " and the element is " + greetings[res]);
        }
  
        else {
  
            Console.WriteLine("The given index is not the start index ");
        }
    }
}
}


Output:

The given index is start index and the element is Hello


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads