Last Updated : 11 Jul, 2025
StringDictionary.Keysproperty is used to get a collection of keys in the StringDictionary.
Syntax:public virtual System.Collections.ICollection Keys { get; }Return Value:
An
ICollectionthat 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 AppleExample 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 & evenNote:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4