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:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
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" );
ICollection ic = myDict.Keys;
foreach (String str in ic)
{
Console.WriteLine(str);
}
}
}
|
Output:
Australia
Belgium
Netherlands
China
Russia
India
Example 2:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
ListDictionary myDict = new ListDictionary();
myDict.Add( "I" , "first" );
myDict.Add( "II" , "second" );
myDict.Add( "III" , "third" );
myDict.Add( "IV" , "fourth" );
myDict.Add( "V" , "fifth" );
ICollection ic = myDict.Keys;
foreach (String str in ic)
{
Console.WriteLine(str);
}
}
}
|
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:
Last Updated :
01 Feb, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...