📌  相关文章
📜  C#|获取一个ICollection,其中包含Hashtable中的值

📅  最后修改于: 2021-05-29 22:07:35             🧑  作者: Mango

Hashtable.Values属性用于获取包含Hashtable中的值的ICollection。

句法:

public virtual System.Collections.ICollection Values { get; }

返回值:该属性返回一个ICollection,其中包含Hashtable中的值。

笔记:

  • ICollection中值的顺序未指定。
  • 检索此属性的值是O(1)操作。

下面的程序说明了上面讨论的属性的用法:

范例1:

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

范例2:

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

参考:

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