C# | OrderedDictionary Class
OrderedDictionary Class represents a collection of key/value pairs that are accessible by the key or index. It is present in System.Collections.Specialized namespace.
Properties of OrderedDictionary Class :
- Each element is a key/value pair stored in a DictionaryEntry object.
- A key cannot be null, but a value can be.
- The elements of an OrderedDictionary are not sorted by the key.
- Elements can be accessed either by the key or by the index.
Constructors
Constructor | Description |
---|---|
OrderedDictionary() | Initializes a new instance of the OrderedDictionary class. |
OrderedDictionary(IEqualityComparer) | Initializes a new instance of the OrderedDictionary class using the specified comparer. |
OrderedDictionary(Int32) | Initializes a new instance of the OrderedDictionary class using the specified initial capacity. |
OrderedDictionary(Int32, IEqualityComparer) | Initializes a new instance of the OrderedDictionary class using the specified initial capacity and comparer. |
OrderedDictionary(SerializationInfo, StreamingContext) | Initializes a new instance of the OrderedDictionary class that is serializable using the specified SerializationInfo and StreamingContext objects. |
Example:
// C# code to create a 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( "1" , "ONE" ); myDict.Add( "2" , "TWO" ); myDict.Add( "3" , "THREE" ); // Displaying the number of key/value // pairs in myDict Console.WriteLine(myDict.Count); // Displaying the key/value pairs in myDict foreach (DictionaryEntry de in myDict) Console.WriteLine(de.Key + " --> " + de.Value); } } |
Output:
3 1 --> ONE 2 --> TWO 3 --> THREE
Properties
Property | Description |
---|---|
Count | Gets the number of key/values pairs contained in the OrderedDictionary collection. |
IsReadOnly | Gets a value indicating whether the OrderedDictionary collection is read-only. |
Item[Int32] | Gets or sets the value at the specified index. |
Item[Object] | Gets or sets the value with the specified key. |
Keys | Gets an ICollection object containing the keys in the OrderedDictionary collection. |
Values | Gets an ICollection object containing the values in the OrderedDictionary collection. |
Example 1:
// C# code to check if OrderedDictionary // collection is read-only 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" ); // Checking if OrderedDictionary // collection is read-only Console.WriteLine(myDict.IsReadOnly); } } |
Output:
False
Example 2:
// C# code to get the number of // key/values pairs contained // in the 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( "A" , "Apple" ); myDict.Add( "B" , "Banana" ); myDict.Add( "C" , "Cat" ); myDict.Add( "D" , "Dog" ); // To Get the number of key/values // pairs contained in the OrderedDictionary Console.WriteLine( "Number of key/value pairs are : " + myDict.Count); } } |
Output:
Number of key/value pairs are : 4
Methods
Method | Description |
---|---|
Add(Object, Object) | Adds an entry with the specified key and value into the OrderedDictionary collection with the lowest available index. |
AsReadOnly() | Returns a read-only copy of the current OrderedDictionary collection. |
Clear() | Removes all elements from the OrderedDictionary collection. |
Contains(Object) | Determines whether the OrderedDictionary collection contains a specific key. |
CopyTo(Array, Int32) | Copies the OrderedDictionary elements to a one-dimensional Array object at the specified index. |
Equals(Object) | Determines whether the specified object is equal to the current object. |
GetEnumerator() | Returns an IDictionaryEnumerator object that iterates through the OrderedDictionary collection. |
GetHashCode() | Serves as the default hash function. |
GetObjectData(SerializationInfo, StreamingContext) | Implements the ISerializable interface and returns the data needed to serialize the OrderedDictionary collection. |
GetType() | Gets the Type of the current instance. |
Insert(Int32, Object, Object) | Inserts a new entry into the OrderedDictionary collection with the specified key and value at the specified index. |
MemberwiseClone() | Creates a shallow copy of the current Object. |
OnDeserialization(Object) | Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete. |
Remove(Object) | Removes the entry with the specified key from the OrderedDictionary collection. |
RemoveAt(Int32) | Removes the entry at the specified index from the OrderedDictionary collection. |
ToString() | Returns a string that represents the current object. |
Example 1:
// C# code to get a read-only // copy of the 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 a read-only copy of // the OrderedDictionary OrderedDictionary myDict_1 = myDict.AsReadOnly(); // Checking if myDict_1 is read-only Console.WriteLine(myDict_1.IsReadOnly); } } |
Output:
True
Example 2:
// C# code to remove the entry // with the specified key from // the 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" ); // Displaying the number of element initially Console.WriteLine( "Number of elements are : " + myDict.Count); // Displaying the elements in myDict foreach (DictionaryEntry de in myDict) Console.WriteLine(de.Key + " -- " + de.Value); // Removing the entry with the specified // key from the OrderedDictionary myDict.Remove( "key2" ); // Displaying the number of element initially Console.WriteLine( "Number of elements are : " + myDict.Count); // Displaying the elements in myDict foreach (DictionaryEntry de in myDict) Console.WriteLine(de.Key + " -- " + de.Value); } } |
Output:
Number of elements are : 5 key1 -- value1 key2 -- value2 key3 -- value3 key4 -- value4 key5 -- value5 Number of elements are : 4 key1 -- value1 key3 -- value3 key4 -- value4 key5 -- value5
Reference:
Please Login to comment...