Index Struct introduced in C# 8.0. It is used to represent a type that can be used as an index of a collection or sequence that starts from either start or end. It provides a new index style to access elements using the ^ (caret) operator. It is used to find the last elements of the specified collection or sequence. Also with the help of the Index struct, we can create a variable of the index type.
- Index-Value: The value property allows us to access the index from the start or the end as an integer by using the Index.Value.
- Index with Range: The Index struct can be used with the Range struct to access the range of elements to be sliced from collections.
- Read-Only: Its value cannot be modified which helps to make consistency.
Example: In this example, we will see how to access elements of an Array from the end using the Index struct.
C#
// Accessing elements using Index Struct
using System;
class Geeks
{
static void Main(string[] args)
{
// Creating and initializing an array
int[] num = new int[] { 1, 2, 3, 4, 5 };
// Accessing elements from the start
Console.WriteLine("Starting Elements");
Console.WriteLine(num[1]);
Console.WriteLine(num[2]);
Console.WriteLine(num[3]);
// Accessing elements from the end
Console.WriteLine("Last Elements");
Console.WriteLine(num[^2]);
Console.WriteLine(num[^3]);
Console.WriteLine(num[^4]);
Console.WriteLine();
// Index as a variable
Index i = ^1;
Console.WriteLine("Index as a variable: " + num[i]);
}
}
Output:
Explanation: In the above example, first we access the elements from the start using the traditional way by normal indexes, then we access elements from the end using the Index struct.
Constructor
Index(Int32, Boolean): It is used to initialize a new Index with a specified index position and a value that indicates if the index is from the start or the end of a collection.
Example:
C#
// C# Program to demonstrate the
// Constructor of Index Struct
class Geeks
{
public static void Main()
{
// Array
var arr = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
// Index Created using the
// Index class constructor
var i = new Index(2);
var i2 = new Index(4, true);
var i3 = new Index(3, false);
Console.WriteLine( arr[i]);
Console.WriteLine( arr[i2]);
Console.WriteLine( arr[i3]);
}
}
Output:
Properties
Property | Description |
---|
End | It is used to get an Index that points beyond the last element. |
IsFromEnd | It is used to get a value that indicates whether the index is from the start or the end. |
isFromStart | It is used to get an Index that points to the first element of a collection. |
Value | It is used to get the index value. |
Example:
C#
// Demonstrating start and end properties
using System;
class Geeks
{
public static void Main()
{
// Array
var arr = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
// Using Index with Start and End properties
Index start = 0;
Index end = ^1;
// Display Start and End values
Console.WriteLine("Start Index Value: " + start.Value);
Console.WriteLine("End Index Value: " + end.Value );
// Display Elements using Index
Console.WriteLine("Element at Start Index: " + arr[start]);
Console.WriteLine("Element at End Index: " + arr[end]);
// Check if the index is from the end
Console.WriteLine("start index from start "
+ start.IsFromEnd);
Console.WriteLine("end index from the end: "
+ end.IsFromEnd);
}
}
Output:
Methods
Method | Description |
---|
Equals() | It is used to check whether the given index is equal to another index or not. |
FromEnd(Int32) | It is used to create an Index from the end of a collection at a specified index position. |
FromStart(Int32) | It is used to create an Index from the specified index at the start of a collection. |
GetHashCode() | It returns the hash code for the given instance. |
GetOffset(Int32) | Used to calculate the offset from the start of the collection using the given collection length. |
ToString() | It is used to return the string representation of the current Index instance. |
Example:
C#
// Demonstrating Equals method with index
using System;
class Geeks
{
public static void Main()
{
// Array
var arr = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
// Using Index with Start and End properties
Index start = Index.FromStart(0);
Index end = Index.FromEnd(1);
// Access elements using Index
Console.WriteLine("Value at start index: " + arr[start]);
Console.WriteLine("Value at end index: " + arr[end]);
// Check if the start and end are equal
Console.WriteLine("The start equals to the end: "+
start.Equals(end));
}
}
Output: