Open In App

How to get the index value in C#?

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 get the index value with the help of Value Property provided by the Index struct.

Syntax:

public int Value { int get(); };

Example 1:




// C# program to illustrate the 
// concept of the value property
using System;
  
namespace example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        // Creating new indexes
        // Using Index() constructor
        var val1 = new Index(1, true);
        var val2 = new Index(2, true);
        var val3 = new Index(1, false);
        var val4 = new Index(3, false);
  
        // Getting the value of the index
        var res1 = val1.Value;
        var res2 = val2.Value;
        var res3 = val3.Value;
        var res4 = val4.Value;
  
        // Display index value
        Console.WriteLine("Index value is : " + res1);
        Console.WriteLine("Index value is : " + res2);
        Console.WriteLine("Index value is : " + res3);
        Console.WriteLine("Index value is : " + res4);
    }
}
}


Output:

Index value is : 1
Index value is : 2
Index value is : 1
Index value is : 3

Example 2:




// C# program to illustrate the 
// concept of the value property
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 index
        // Using Index() constructor
        var val1 = new Index(1, true);
        var val2 = new Index(2, false);
  
        // Checking the given both the 
        // index values are equal or not
        if (val1.Value.Equals(val2.Value) == true
        {
            Console.WriteLine("Both the indexes are equal and"+
              " their elements are : {0}, {1}", greetings[val1],
                                              greetings[val2]);
        }
  
        else {
            Console.WriteLine("Both the indexes are not equal"+
           " and their elements are : {0}, {1}", greetings[val1],
                                               greetings[val2]);
        }
    }
}
}


Output:

Both the indexes are not equal and their elements are : Ahnyounghaseyo, Namaste


Last Updated : 28 Nov, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads