C# | Gets an ICollection containing the values in the Hashtable

Hashtable.Values Property is used to get an ICollection containing the values in the Hashtable.

Syntax:

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

Return Value: This property returns an ICollection containing the values in the Hashtable.

Note:

  • The order of values in the ICollection is unspecified.
  • Retrieving the value of this property is an O(1) operation.

Below programs illustrate the use of above-discussed property:

Example 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", "Beginner");
        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]);
    }
}


Output:

data structures
c++
quiz
Beginner

Example 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]);
    }
}


Output:

City
Country
Country
Planet

Reference:

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


Contact Us