C# | Get or set the element at specified index in Collection<T>
Collection<T>.Item[Int32] property is used to get or set the element at the specified index.
Syntax:
public T this[int index] { get; set; }
Here, index is the zero-based index of the element to get or set.
Return Value: The element at the specified index.
Exception: This method will give ArgumentOutOfRangeException if the index is less than zero or index is equal to or greater than Count.
Below given are some examples to understand the implementation in a better way:
Example 1:
// C# code to get or set the // element at the specified index using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of strings Collection<string> myColl = new Collection<string>(); // Adding elements in Collection myColl myColl.Add("A"); myColl.Add("B"); myColl.Add("C"); myColl.Add("D"); myColl.Add("E"); // Displaying the elements in myColl foreach(string str in myColl) { Console.WriteLine(str); } // Get the element at index 2 Console.WriteLine("Element at index 2 is : " + myColl[2]); // Get the element at index 3 Console.WriteLine("Element at index 3 is : " + myColl[3]); // Set the element at index 3 myColl[3] = "GFG"; // Displaying the elements in myColl foreach(string str in myColl) { Console.WriteLine(str); } } } |
Output:
A B C D E Element at index 2 is : C Element at index 3 is : D A B C GFG E
Example 2:
// C# code to get or set the // element at the specified index using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of ints Collection<int> myColl = new Collection<int>(); // Adding elements in Collection myColl myColl.Add(2); myColl.Add(3); myColl.Add(4); myColl.Add(5); // Displaying the elements in myColl foreach(int i in myColl) { Console.WriteLine(i); } // Get the element at index -1 // This should raise "ArgumentOutOfRangeException" // as the index is less than 0 Console.WriteLine("Element at index -1 is : " + myColl[-1]); // Set the element at index 2 myColl[2] = 10; // Displaying the elements in myColl foreach(int i in myColl) { Console.WriteLine(i); } } } |
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
Note:
- Collection<T> accepts null as a valid value for reference types and allows duplicate elements.
- This property provides the ability to access a specific element in the collection by using the syntax : myCollection[index].
- Retrieving the value of this property is an O(1) operation.
- Setting the property is also an O(1) operation.
Reference:
Recommended Posts:
- C# | Get or set the element at the specified index in ArrayList
- C# | Gets or sets the element at the specified index in StringCollection
- 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# | Gets or Sets the element at the specified index in the List
- C# | Remove the element at the specified index of the ArrayList
- C# | Remove element at specified index of Collection<T>
- C# | Finding the index of last element in the array
- C# | Finding the index of first element in the array
- C# | Insert an element into Collection<T> at specified index
- C# | Get or Set at specified index in StringCollection
- C# | Getting the key at the specified index of a SortedList object
- C# | Remove from the specified index of a SortedList
- C# | Insert at the specified index in StringCollection
- C# | Getting the index of the specified key in a SortedList object
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.



