C# | OrderedDictionary.Item[Object] Property

There are two methods in the overload list of this property as follows:

  1. Item[Int32] : Used to get or set the value at the specified index.
  2. 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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]);
    }
}

chevron_right


Output:

value2

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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]);
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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"]);
    }
}

chevron_right


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:



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

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.