C# | Getting the Values in a SortedList object
Last Updated :
01 Feb, 2019
SortedList.Values Property is used to get the values in a SortedList object.
Syntax:
public virtual System.Collections.ICollection Values { get; }
Property Value: An ICollection object containing the values in the SortedList object.
Below programs illustrate the use of above-discussed property:
Example 1:
using System;
using System.Collections;
class GFG {
public static void Main()
{
SortedList mylist = new SortedList();
mylist.Add( "g" , "geeks" );
mylist.Add( "c" , "c++" );
mylist.Add( "d" , "data structures" );
mylist.Add( "q" , "quiz" );
ICollection c = mylist.Values;
foreach ( string str in c)
Console.WriteLine(str + mylist[str]);
}
}
|
Output:
c++
data structures
geeks
quiz
Example 2:
using System;
using System.Collections;
class GFG {
public static void Main()
{
SortedList mylist = new SortedList();
mylist.Add( "India" , "Country" );
mylist.Add( "Chandigarh" , "City" );
mylist.Add( "Mars" , "Planet" );
mylist.Add( "China" , "Country" );
ICollection c = mylist.Values;
foreach ( string str in c)
Console.WriteLine(str + mylist[str]);
}
}
|
Output:
City
Country
Country
Planet
Note:
- The ICollection object is a read-only view of the values of the SortedList object. Modifications made to the underlying SortedList are immediately reflected in the ICollection.
- The elements of the ICollection are sorted in the same order as the values of the SortedList.
- This property is similar to the GetValueList method but returns an ICollection object instead of an IList object.
- This method is an O(1) operation.
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...