C# | Getting the Values in a SortedList object

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to get an ICollection containing
// the values in the SortedList
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedList
        SortedList mylist = new SortedList();
  
        // Adding elements in SortedList
        mylist.Add("g", "geeks");
        mylist.Add("c", "c++");
        mylist.Add("d", "data structures");
        mylist.Add("q", "quiz");
  
        // Get a collection of the values
        ICollection c = mylist.Values;
  
        // Displaying the contents
        foreach(string str in c)
            Console.WriteLine(str + mylist[str]);
    }
}

chevron_right


Output:

c++
data structures
geeks
quiz

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to get an ICollection containing
// the values in the SortedList
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedList
        SortedList mylist = new SortedList();
  
        // Adding elements in SortedList
        mylist.Add("India", "Country");
        mylist.Add("Chandigarh", "City");
        mylist.Add("Mars", "Planet");
        mylist.Add("China", "Country");
  
        // Get a collection of the values
        ICollection c = mylist.Values;
  
        // Displaying the contents
        foreach(string str in c)
            Console.WriteLine(str + mylist[str]);
    }
}

chevron_right


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:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.