Finding the Index which Points Beyond the Last Element 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 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:
using System;
namespace example {
class GFG {
static void Main( string [] args)
{
var in1 = new Index(1, true );
var in2 = new Index(3, false );
var res1 = Index.End;
Console.WriteLine( "Index: {0}" , in1);
Console.WriteLine( "Index: {0}" , in2);
Console.WriteLine( "End Index: {0}" , res1);
}
}
}
|
Output:
Index: ^1
Index: 3
End Index: ^0
Example 2:
using System;
namespace example {
class GFG {
static void Main( string [] args)
{
string [] greetings = new string [] { "Hello" , "Hola" , "Namaste" ,
"Bonjour" , "Ohayo" , "Ahnyounghaseyo" };
var res = Index.End;
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" );
}
}
}
}
|
Output:
The given index is beyond the last element
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...