Creating an Index From the Specified Index at the Start of a Collection in C#

In C#, manipulating collections is a frequent operation, with indexing being a crucial part of this process. The Index struct, introduced in C# 8.0, provides a powerful way to create indices that can represent positions from the start or end of a collection. This article will guide you through creating and using indices from specified positions at the start of collections in C#.

Syntax

Following is the syntax for creating an Index from the start of a collection

Index index = new Index(value, fromEnd: false);
// OR using implicit conversion
Index index = value;

Following is the syntax for using the index with collections

collection[index]
collection[^index]  // from end syntax

Understanding Traditional Indexing in C#

In traditional C# indexing, elements in arrays and collections are accessed using zero-based indices, where the first element is at position 0

Array Indexing from Start 1 2 3 4 5 0 1 2 3 4 Index positions

Example

using System;

class Program {
   static void Main() {
      int[] numbers = { 1, 2, 3, 4, 5 };

      Console.WriteLine("Element at index 0: " + numbers[0]);
      Console.WriteLine("Element at index 2: " + numbers[2]);
      Console.WriteLine("Element at index 4: " + numbers[4]);
   }
}

The output of the above code is

Element at index 0: 1
Element at index 2: 3
Element at index 4: 5

Using the Index Struct

The Index struct allows you to create more expressive and flexible indexing patterns. You can create an index from the start by using a non-negative integer value

Example

using System;

class Program {
   static void Main() {
      int[] numbers = { 10, 20, 30, 40, 50 };

      Index startIndex = new Index(1, fromEnd: false);  // Index from start
      Index implicitIndex = 3;  // Implicit conversion to Index

      Console.WriteLine("Using explicit Index: " + numbers[startIndex]);
      Console.WriteLine("Using implicit Index: " + numbers[implicitIndex]);
      
      // Direct integer indexing still works
      Console.WriteLine("Direct indexing: " + numbers[0]);
   }
}

The output of the above code is

Using explicit Index: 20
Using implicit Index: 40
Direct indexing: 10

Using Index with Different Collection Types

The Index struct works with arrays, strings, lists, and other indexable collections

Example

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      // With List
      List<string> fruits = new List<string> { "Apple", "Banana", "Cherry", "Date" };
      Index listIndex = 2;
      Console.WriteLine("Fruit at index 2: " + fruits[listIndex]);

      // With string
      string text = "Programming";
      Index stringIndex = 4;
      Console.WriteLine("Character at index 4: " + text[stringIndex]);

      // With array
      double[] prices = { 10.5, 25.0, 15.75, 30.25 };
      Index arrayIndex = 1;
      Console.WriteLine("Price at index 1: " + prices[arrayIndex]);
   }
}

The output of the above code is

Fruit at index 2: Cherry
Character at index 4: r
Price at index 1: 25

Creating Dynamic Indices

You can create indices dynamically based on runtime conditions, making your code more flexible

Example

using System;

class Program {
   static void Main() {
      int[] data = { 100, 200, 300, 400, 500, 600 };
      
      // Create indices based on conditions
      for (int i = 0; i < 3; i++) {
         Index currentIndex = i * 2;  // Even indices: 0, 2, 4
         Console.WriteLine($"Element at index {currentIndex.Value}: {data[currentIndex]}");
      }
      
      // Using Index properties
      Index midIndex = data.Length / 2;
      Console.WriteLine($"Middle element: {data[midIndex]}");
      Console.WriteLine($"Is from end: {midIndex.IsFromEnd}");
   }
}

The output of the above code is

Element at index 0: 100
Element at index 2: 300
Element at index 4: 500
Middle element: 400
Is from end: False

Comparison: Index vs Traditional Indexing

Feature Traditional Indexing Index Struct
Syntax array[2] array[index] where index = 2
Type Safety Uses int directly Strongly typed Index struct
Direction Support From start only From start or end
Runtime Information Limited Provides IsFromEnd, Value properties

Conclusion

The Index struct in C# provides a powerful and flexible way to create indices from specified positions at the start of collections. While traditional integer indexing remains valid, the Index struct offers better type safety, runtime information, and supports both forward and backward indexing patterns for more expressive collection manipulation.

Updated on: 2026-03-17T07:04:36+05:30

475 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements