C# | OrderedDictionary.Item[Object] Property
Last Updated :
16 Sep, 2021
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
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
OrderedDictionary myDict = new OrderedDictionary();
myDict.Add( "key1" , "value1" );
myDict.Add( "key2" , "value2" );
myDict.Add( "key3" , "value3" );
myDict.Add( "key4" , "value4" );
myDict.Add( "key5" , "value5" );
Console.WriteLine(myDict[1]);
}
}
|
Output:
value2
Example 2:
CSHARP
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
OrderedDictionary myDict = new OrderedDictionary();
myDict.Add( "key1" , "value1" );
myDict.Add( "key2" , "value2" );
myDict.Add( "key3" , "value3" );
myDict.Add( "key4" , "value4" );
myDict.Add( "key5" , "value5" );
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
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
OrderedDictionary myDict = new OrderedDictionary();
myDict.Add( "key1" , "value1" );
myDict.Add( "key2" , "value2" );
myDict.Add( "key3" , "value3" );
myDict.Add( "key4" , "value4" );
myDict.Add( "key5" , "value5" );
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:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...