C# | Get a collection of values in the StringDictionary
Last Updated :
01 Feb, 2019
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:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringDictionary myDict = new StringDictionary();
myDict.Add( "A" , "Apple" );
myDict.Add( "B" , "Banana" );
myDict.Add( "C" , "Cat" );
myDict.Add( "D" , "Dog" );
foreach ( string val in myDict.Values)
{
Console.WriteLine(val);
}
}
}
|
Output:
Dog
Banana
Cat
Apple
Example 2:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringDictionary myDict = new StringDictionary();
myDict.Add( "3" , "prime & odd" );
myDict.Add( "2" , "prime & even" );
myDict.Add( "4" , "non-prime & even" );
myDict.Add( "9" , "non-prime & odd" );
foreach ( string val in myDict.Values)
{
Console.WriteLine(val);
}
}
}
|
Output:
prime & even
prime & odd
non-prime & odd
non-prime & even
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 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:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...