C# | Getting the list of Values of a SortedList object

SortedList.GetValueList Method is used to get the list of keys in a SortedList object.

Syntax:

public virtual System.Collections.IList GetValueList ();

Return Value: It returns an IList object containing the values in the SortedList object.

Below programs illustrate the use of above-discussed method:

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code for getting the values
// in a SortedList object
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a SortedList of integers
        SortedList mylist = new SortedList();
  
        // Adding elements to SortedList
        mylist.Add("1", "C++");
        mylist.Add("2", "Java");
        mylist.Add("3", "DSA");
        mylist.Add("4", "Python");
        mylist.Add("5", "C#");
  
        // taking an IList and
        // using GetValueList method
        IList vlist = mylist.GetValueList();
  
        // Prints the list of keys
        Console.WriteLine("Value Stored in SortedList:");
  
        // will print the values in Sorted Order
        for (int i = 0; i < mylist.Count; i++)
            Console.WriteLine(vlist[i]);
    }
}

chevron_right


Output:

Value Stored in SortedList:
C++
Java
DSA
Python
C#

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code for getting the values
// in a SortedList object
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a SortedList of integers
        SortedList mylist = new SortedList();
  
        // Adding elements to SortedList
        mylist.Add("First", "Ram");
        mylist.Add("Second", "Shyam");
        mylist.Add("Third", "Mohit");
        mylist.Add("Fourth", "Rohit");
        mylist.Add("Fifth", "Manish");
  
        // taking an IList and
        // using GetValueList method
        IList vlist = mylist.GetValueList();
  
        // Prints the list of keys
        Console.WriteLine("Value Stored in SortedList:");
  
        // will print the values in Sorted Order
        for (int i = 0; i < mylist.Count; i++)
            Console.WriteLine(vlist[i]);
    }
}

chevron_right


Output:

Value Stored in SortedList:
Manish
Ram
Rohit
Shyam
Mohit

Note:

  • The returned IList object is a read-only view of the values of the SortedList object. Modifications made to the underlying SortedList are immediately reflected in the IList.
  • The elements of the returned IList are sorted in the same order as the values of the SortedList.
  • This method is similar to the Values property but returns an IList object instead of an ICollection 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.