C# | Check if OrderedDictionary collection contains a specific key Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report OrderedDictionary.Contains(Object) method is used to check whether the OrderedDictionary collection contains a specific key or not. Syntax: public bool Contains (object key); Here, key is the key to locate in the OrderedDictionary collection. Return Value: This method returns True if the OrderedDictionary collection contains an element with the specified key, otherwise, False. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to check if OrderedDictionary // collection contains a specific 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"); // Checking if OrderedDictionary // collection contains a specific key Console.WriteLine(myDict.Contains("Key6")); } } Output: False Example 2: CSHARP // C# code to check if OrderedDictionary // collection contains a specific 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"); // Displaying the key/value pairs in myDict foreach(DictionaryEntry de in myDict) Console.WriteLine(de.Key + " --> " + de.Value); // Checking if OrderedDictionary collection // contains a specific key if (myDict.Contains("key4")) myDict.Remove("key4"); // Displaying the key/value pairs in myDict foreach(DictionaryEntry de in myDict) Console.WriteLine(de.Key + " --> " + de.Value); } } Output: key1 --> value1 key2 --> value2 key3 --> value3 key4 --> value4 key5 --> value5 key1 --> value1 key2 --> value2 key3 --> value3 key5 --> value5 Note: Using the Item[Object] property can return a null value if the key does not exist or if the key is null. Use the Contains method to determine if a specific key exists in the OrderedDictionary collection. Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.ordereddictionary.contains?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Check if OrderedDictionary collection is read-only S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Specialized-Namespace CSharp-Specialized-OrderedDictionary Similar Reads C# | Check if ListDictionary contains a specific key ListDictionary.Contains(Object) method is used to check whether the ListDictionary contains a specific key or not. Syntax: public bool Contains (object key); Here, key is the key to locate in the ListDictionary. Return Value: The method returns true if the ListDictionary contains an entry with the s 2 min read C# | Check if OrderedDictionary collection is read-only OrderedDictionary.IsReadOnly property is used to get a value that indicates whether the OrderedDictionary collection is read-only or not. Syntax : public bool IsReadOnly { get; } Return Value: This property returns True if the OrderedDictionary collection is read-only, otherwise, False. The default 2 min read C# | Check if SortedDictionary contains the specified key or not SortedDictionary<TKey, TValue>.ContainsKey(TKey) Method is used to check whether the SortedDictionary contains an element with the specified key or not. Syntax: public bool ContainsKey (TKey key); Here, the key is the Key which is to be located in the SortedDictionary. Returns Value: This meth 2 min read C# | Add key and value into OrderedDictionary collection OrderedDictionary.Add(Object, Object) method is used to add an entry with the specified key and value into the OrderedDictionary collection with the lowest available index. Syntax: public void Add (object key, object value); Parameters: key : The key of the entry to add. value : The value of the ent 2 min read C# | Get an ICollection containing keys in OrderedDictionary OrderedDictionary.Keys property is used to get an ICollection object containing the keys in the OrderedDictionary collection. Syntax: public System.Collections.ICollection Keys { get; } Return Value: It returns an ICollection object containing the keys in the OrderedDictionary collection. Below give 2 min read C# | Check if SortedSet and the specified collection contain the same elements SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet.SetEquals(IEnumerable) Method is used to check whether the SortedSet and the specified collection contain the same elements. Properties: In C#, SortedSet c 2 min read Like