Gets or sets the element at the specified index in StringCollection in C#

The StringCollection class in C# provides an indexer property that allows you to get or set elements at a specified index. This indexer uses square bracket notation and provides direct access to collection elements by their zero-based index position.

Syntax

Following is the syntax for accessing elements by index in StringCollection

// Getting an element
string element = stringCollection[index];

// Setting an element
stringCollection[index] = "new value";

Parameters

  • index − The zero-based index of the element to get or set.

Return Value

Returns the string element at the specified index when getting, or allows assignment of a string value when setting.

Getting Elements by Index

The following example demonstrates how to retrieve elements from specific positions in a StringCollection

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringCollection strCol = new StringCollection();
      String[] strArr = new String[] { "A", "B", "C", "D", "E", "F", "G", "H" };
      
      Console.WriteLine("StringCollection elements...");
      foreach (string str in strArr) {
         Console.WriteLine(str);
      }
      
      strCol.AddRange(strArr);
      Console.WriteLine("Element at 5th index = " + strCol[5]);
      Console.WriteLine("Count of strings in StringCollection = " + strCol.Count);
   }
}

The output of the above code is −

StringCollection elements...
A
B
C
D
E
F
G
H
Element at 5th index = F
Count of strings in StringCollection = 8

Setting Elements by Index

The following example shows how to modify elements at specific positions using the indexer −

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringCollection strCol = new StringCollection();
      String[] strArr = new String[] { "A", "B", "C", "D", "E", "F", "G", "H" };
      
      Console.WriteLine("Original StringCollection elements...");
      foreach (string str in strArr) {
         Console.WriteLine(str);
      }
      
      strCol.AddRange(strArr);
      Console.WriteLine("Element at 5th index = " + strCol[5]);
      
      strCol[5] = "K";
      Console.WriteLine("Element at 5th index [UPDATED] = " + strCol[5]);
      
      Console.WriteLine("Updated StringCollection:");
      for (int i = 0; i < strCol.Count; i++) {
         Console.WriteLine("Index " + i + ": " + strCol[i]);
      }
   }
}

The output of the above code is −

Original StringCollection elements...
A
B
C
D
E
F
G
H
Element at 5th index = F
Element at 5th index [UPDATED] = K
Updated StringCollection:
Index 0: A
Index 1: B
Index 2: C
Index 3: D
Index 4: E
Index 5: K
Index 6: G
Index 7: H

Key Rules

  • Index values are zero-based, meaning the first element is at index 0.

  • Accessing an index that is out of range throws an ArgumentOutOfRangeException.

  • Setting a value to null is allowed and stores an empty string in the collection.

  • The indexer provides both read and write access to collection elements.

Conclusion

The StringCollection indexer provides convenient direct access to elements using square bracket notation. This allows both retrieving and modifying string values at specific zero-based index positions, making it easy to work with string collections when you know the exact position of elements.

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

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements