How to get the index value in C#?
Last Updated :
28 Nov, 2019
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:
using System;
namespace example {
class GFG {
static void Main( string [] args)
{
var val1 = new Index(1, true );
var val2 = new Index(2, true );
var val3 = new Index(1, false );
var val4 = new Index(3, false );
var res1 = val1.Value;
var res2 = val2.Value;
var res3 = val3.Value;
var res4 = val4.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:
using System;
namespace example {
class GFG {
static void Main( string [] args)
{
string [] greetings = new string [] { "Hello" , "Hola" , "Namaste" ,
"Bonjour" , "Ohayo" , "Ahnyounghaseyo" };
var val1 = new Index(1, true );
var val2 = new Index(2, false );
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
Share your thoughts in the comments
Please Login to comment...