There are two methods in the overload list of this property as follows:
- Item[Int32] : Used to get or set the value at the specified index.
- Item[Object] : Used to get or set the value with the specified key.
OrderedDictionary.Item[Int32] Property
This property is used to get or set the value at the specified index.
Syntax:
public object this[int index] { get; set; }
Here, index is the zero-based index of the value to get or set.
Return Value: The value of the item at the specified index.
Exceptions:
- NotSupportedException : If the OrderedDictionary collection is read-only.
- 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 value // at the specified index for // OrderedDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver method public static void Main() { // Creating a orderedDictionary named myDict OrderedDictionary myDict = new OrderedDictionary(); // Adding key and value in myDict myDict.Add("key1", "value1"); myDict.Add("key2", "value2"); myDict.Add("key3", "value3"); myDict.Add("key4", "value4"); myDict.Add("key5", "value5"); // To get or set the value at the // specified index for OrderedDictionary Console.WriteLine(myDict[1]); } } |
Output:
value2
Example 2:
// C# code to get or set the value // at the specified index for // OrderedDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver method public static void Main() { // Creating a orderedDictionary named myDict OrderedDictionary myDict = new OrderedDictionary(); // Adding key and value in myDict myDict.Add("key1", "value1"); myDict.Add("key2", "value2"); myDict.Add("key3", "value3"); myDict.Add("key4", "value4"); myDict.Add("key5", "value5"); // To get or set the value at the // specified index for OrderedDictionary // This should raise "ArgumentOutOfRangeException" // as index is less than 0 Console.WriteLine(myDict[-3]); } } |
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: This property allows you to access a specific element in the collection by using the syntax : myCollection[index].
OrderedDictionary.Item[Object] Property
This property is used to get or set the value with the specified key.
Syntax:
public object this[object key] { get; set; }
Here, key is the key of the value to get or set.
Return Value: The value associated with the specified key. If the specified key is not found, attempting to get it returns null, and attempting to set it creates a new element using the specified key.
Exception : This prooperty will give NotSupportedException if the property is being set and the OrderedDictionary collection is read-only.
Example:
// C# code to get or set the // value with the specified key using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver method public static void Main() { // Creating a orderedDictionary named myDict OrderedDictionary myDict = new OrderedDictionary(); // Adding key and value in myDict myDict.Add("key1", "value1"); myDict.Add("key2", "value2"); myDict.Add("key3", "value3"); myDict.Add("key4", "value4"); myDict.Add("key5", "value5"); // To get or set the value // with the specified key Console.WriteLine(myDict["key3"]); } } |
Output:
value3
Note:
- This property allows you to access a specific element in the collection by using the syntax : myCollection[key].
- A key cannot be null, but a value can be.
Reference:
Recommended Posts:
- How to create a StringDictionary in C#
- How to create a ListDictionary in C#
- How to create a StringCollection in C#
- How to create an OrderedDictionary in C#
- How to get Synchronize access to the HybridDictionary in C#
- How to get Synchronize access to the StringCollection in C#
- How to get Synchronize access to the ListDictionary in C#
- How to get Synchronize access to the StringDictionary in C#
- C# | Check if two StringCollection objects are equal
- C# | Gets or sets the value at the specified key in StringDictionary
- C# | Check if two OrderedDictionary objects are equal
- C# | Gets or sets the element at the specified index in StringCollection
- C# | Check if two ListDictionary objects are equal
- C# | Check if two StringDictionary objects are equal or not
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.

