C# | Get an ICollection containing the values in HybridDictionary
Last Updated :
01 Feb, 2019
HybridDictionary.Values property is used to get an ICollection containing the values in the HybridDictionary.
Syntax:
public System.Collections.ICollection Values { get; }
Return Value: It returns an ICollection containing the values in the HybridDictionary.
Below programs illustrate the use of HybridDictionary.Values property:
Example 1:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
HybridDictionary myDict = new HybridDictionary();
myDict.Add( "A" , "Apple" );
myDict.Add( "B" , "Banana" );
myDict.Add( "C" , "Cat" );
myDict.Add( "D" , "Dog" );
myDict.Add( "E" , "Elephant" );
myDict.Add( "F" , "Fish" );
String[] myArr = new String[myDict.Count];
myDict.Values.CopyTo(myArr, 0);
for ( int i = 0; i < myDict.Count; i++)
Console.WriteLine(myArr[i]);
}
}
|
Output:
Apple
Banana
Cat
Dog
Elephant
Fish
Example 2:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
HybridDictionary myDict = new HybridDictionary();
myDict.Add( "I" , "first" );
myDict.Add( "II" , "second" );
myDict.Add( "III" , "third" );
myDict.Add( "IV" , "fourth" );
myDict.Add( "V" , "fifth" );
String[] myArr = new String[myDict.Count];
myDict.Values.CopyTo(myArr, 0);
for ( int i = 0; i < myDict.Count; i++)
Console.WriteLine(myArr[i]);
}
}
|
Output:
first
second
third
fourth
fifth
Note:
- The order of the values in the ICollection is unspecified, but it is the same order as the associated keys in the ICollection returned by the Keys method.
- The returned ICollection is not a static copy. Instead, the ICollection refers back to the values in the original HybridDictionary. Therefore, changes to the HybridDictionary continue to be reflected in the ICollection.
- Retrieving the value of this property is an O(1) operation.
Reference:
Share your thoughts in the comments
Please Login to comment...