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 ListDictionary.Keys property:
Example 1:
// C# code to get an ICollection // containing the keys in the ListDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a ListDictionary named myDict ListDictionary myDict = new ListDictionary(); myDict.Add("Australia", "Canberra"); myDict.Add("Belgium", "Brussels"); myDict.Add("Netherlands", "Amsterdam"); myDict.Add("China", "Beijing"); myDict.Add("Russia", "Moscow"); myDict.Add("India", "New Delhi"); // Get an ICollection containing // the keys in the ListDictionary ICollection ic = myDict.Keys; // Displaying the keys in ICollection ic foreach(String str in ic) { Console.WriteLine(str); } } } |
chevron_right
filter_none
Output:
Australia Belgium Netherlands China Russia India
Example 2:
// C# code to get an ICollection // containing the keys in the ListDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a ListDictionary named myDict ListDictionary myDict = new ListDictionary(); myDict.Add("I", "first"); myDict.Add("II", "second"); myDict.Add("III", "third"); myDict.Add("IV", "fourth"); myDict.Add("V", "fifth"); // Get an ICollection containing // the keys in the ListDictionary ICollection ic = myDict.Keys; // Displaying the keys in ICollection ic foreach(String str in ic) { Console.WriteLine(str); } } } |
chevron_right
filter_none
Output:
I II III IV V
Note:
- The order of the values 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 ListDictionary. Therefore, changes to the ListDictionary continue to be reflected in the ICollection.
- Retrieving the value of this property is an O(1) operation.
Reference:
Recommended Posts:
- C# | Get an ICollection containing the values in ListDictionary
- C# | Get an ICollection containing keys in OrderedDictionary
- C# | Gets an ICollection containing the keys in the Hashtable
- C# | Get an ICollection containing the keys in HybridDictionary
- C# | Gets an ICollection containing the values in the Hashtable
- C# | Get an ICollection containing the values in HybridDictionary
- C# | Get an ICollection containing values in OrderedDictionary
- C# | Get or set the value associated with specified key in ListDictionary
- C# | Add the specified key and value into the ListDictionary
- How to create a ListDictionary in C#
- C# | ListDictionary Class
- How to get Synchronize access to the ListDictionary in C#
- C# | Remove the entry with specified key from ListDictionary
- C# | Check if ListDictionary is read-only
- C# | Get an enumerator that iterates through the ListDictionary
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.



