📜  C#|获取SortedList对象的值列表

📅  最后修改于: 2021-05-29 17:42:08             🧑  作者: Mango

SortedList.GetValueList方法用于获取SortedList对象中的键列表。

句法:

public virtual System.Collections.IList GetValueList ();

返回值:返回一个IList对象,其中包含SortedList对象中的值。

下面的程序说明了上面讨论的方法的使用:

范例1:

// 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]);
    }
}
输出:
Value Stored in SortedList:
C++
Java
DSA
Python
C#

范例2:

// 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]);
    }
}
输出:
Value Stored in SortedList:
Manish
Ram
Rohit
Shyam
Mohit

笔记:

  • 返回的IList对象是SortedList对象的值的只读视图。对基础SortedList的修改将立即反映在IList中。
  • 返回的IList的元素以与SortedList的值相同的顺序排序。
  • 此方法类似于Values属性,但返回一个IList对象,而不是ICollection对象。
  • 此方法是O(1)操作。

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.sortedlist.getvaluelist?view=netframework-4.7.2