C# | OrderedDictionary.Item[Object] Property
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:
CSHARP
// 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:
CSHARP
// 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 property will give NotSupportedException if the property is being set and the OrderedDictionary collection is read-only.
Example:
CSHARP
// 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:
- https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.ordereddictionary.item?view=netframework-4.7.2