C# | Get a collection of keys in the StringDictionary Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report StringDictionary.Keys property is used to get a collection of keys in the StringDictionary. Syntax: public virtual System.Collections.ICollection Keys { get; } Return Value: An ICollection that provides the keys in the StringDictionary. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to get a collection // of keys in the StringDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named myDict StringDictionary myDict = new StringDictionary(); // Adding key and value into the StringDictionary myDict.Add("A", "Apple"); myDict.Add("B", "Banana"); myDict.Add("C", "Cat"); myDict.Add("D", "Dog"); // To copy the StringDictionary values to // a one-dimensional Array instance at // the specified index. String[] myKeys = new String[myDict.Count]; myDict.Keys.CopyTo(myKeys, 0); // Getting a collection of keys // in the StringDictionary for (int i = 0; i < myDict.Count; i++) { Console.WriteLine(myKeys[i] + " " + myDict[myKeys[i]]); } } } Output: d Dog b Banana c Cat a Apple Example 2: CSHARP // C# code to get a collection // of keys in the StringDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named myDict StringDictionary myDict = new StringDictionary(); // Adding key and value into the StringDictionary myDict.Add("3", "prime & odd"); myDict.Add("2", "prime & even"); myDict.Add("4", "non-prime & even"); myDict.Add("9", "non-prime & odd"); // To copy the StringDictionary values to // a one-dimensional Array instance at // the specified index. String[] myKeys = new String[myDict.Count]; myDict.Keys.CopyTo(myKeys, 0); // Getting a collection of keys // in the StringDictionary for (int i = 0; i < myDict.Count; i++) { Console.WriteLine(myKeys[i] + " " + myDict[myKeys[i]]); } } } Output: 2 prime & even 3 prime & odd 9 non-prime & odd 4 non-prime & even Note: The order of the keys in the ICollection is unspecified, but it is the same order as the associated values in the ICollection returned by the Values method. The returned ICollection is not a static copy. Instead, the ICollection refers back to the keys in the original StringDictionary. Therefore, changes to the StringDictionary continue to be reflected in the ICollection. Retrieving the value of this property is an O(1) operation. Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.stringdictionary.keys?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Get an ICollection containing the keys in ListDictionary S Sahil_Bansall Follow Improve Article Tags : C# CSharp-Collections-Namespace CSharp-Specialized-StringDictionary CSharp-Specialized-Namespace Similar Reads C# | Get a collection of values in the StringDictionary StringDictionary.Values property is used to get a collection of values in the StringDictionary. Syntax: public virtual System.Collections.ICollection Values { get; } Return Value: An ICollection that provides the values in the StringDictionary. Example 1: CSHARP // C# code to get a collection // of 2 min read C# | Check if the StringDictionary contains a specific key StringDictionary.ContainsKey(String) method is used to check whether the StringDictionary contains a specific key or not. Syntax: public virtual bool ContainsKey (string key); Here, key is the key to locate in the StringDictionary. Return Value: This method returns true if the StringDictionary conta 2 min read C# | Get an ICollection containing the keys in ListDictionary ListDictionary.Keys property is used to get an ICollection containing the keys in the ListDictionary. Syntax: public System.Collections.ICollection Keys { get; } Return Value : It returns an ICollection containing the keys in the ListDictionary. Below are the programs to illustrate the use of ListDi 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 How to create a StringDictionary in C# StringDictionary() constructor is used to initialize a new instance of the StringDictionary class which will be empty and will have the default initial capacity. StringDictionary is a specialized collection. This class comes under the System.Collections.Specialized namespace. It only allows string k 2 min read C# | Check if two StringDictionary objects are equal or not Equals(Object) Method which is inherited from the Object class is used to check if a specified StringDictionary object is equal to another StringDictionary object or not. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is to be compared with the current object. Return 2 min read Like