C# | Gets or sets the element at the specified index in StringCollection
StringCollection.Item[Int32] Property is used to get or set the element at the specified index.
Syntax:
public string this[int index] { get; set; }
Here, index is the zero-based index of the entry to get or set.
Return Value: It returns the element of String type at the specified index.
Exception: This property throws ArgumentOutOfRangeException if the index is less than zero or index is equal to or greater than Count.
Below programs illustrate the use of above-discussed property:
Example 1:
// C# code to get or set the element at // the specified index in StringCollection using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // creating a StringCollection named myCol StringCollection myCol = new StringCollection(); // Adding elements in StringCollection myCol.Add("A"); myCol.Add("B"); myCol.Add("C"); myCol.Add("D"); myCol.Add("E"); // Displaying objects in myCol foreach(Object obj in myCol) { Console.WriteLine(obj); } Console.WriteLine("\nAfter Item[int32] Property: \n"); // setting the value at index 2 myCol[2] = "Z"; // Displaying the elements // in the StringCollection foreach(Object obj1 in myCol) { Console.WriteLine(obj1); } } } |
Output:
A B C D E After Item[int32] Property: A B Z D E
Example 2:
// C# code to get or set the element at // the specified index in StringCollection using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // creating a StringCollection named myCol StringCollection myCol = new StringCollection(); // Adding elements in StringCollection myCol.Add("Geeks"); myCol.Add("GFG"); myCol.Add("DS"); myCol.Add("Class"); myCol.Add("Noida"); // Displaying objects in myCol foreach(Object obj in myCol) { Console.WriteLine(obj); } Console.WriteLine("\nAfter Item[int32] Property: \n"); // setting the value at index 8 // this will give error as index // is greater than count myCol[8] = "C#"; // Displaying the elements // in the StringCollection foreach(Object obj1 in myCol) { Console.WriteLine(obj1); } } } |
Runtime Error:
Unhandled Exception:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Reference:
Recommended Posts:
- C# | Gets or Sets the element at the specified index in the List
- C# | Get or Set at specified index in StringCollection
- C# | Remove from the specified index of the StringCollection
- C# | Index of first occurrence in StringCollection
- C# | Insert at the specified index in StringCollection
- C# | Copy StringCollection at the specified index of array
- C# | Get or set the element at specified index in Collection<T>
- C# | Get or set the element at the specified index in ArrayList
- C# | Insert an element into Collection<T> at specified index
- C# | How to remove the element from the specified index of the List
- C# | Insert an element into the ArrayList at the specified index
- C# | Remove the element at the specified index of the ArrayList
- C# | Finding the index of first element in the array
- C# | Finding the index of last element in the array
- C# | Remove element at specified index of Collection<T>
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



