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

The Hashtable.Values property in C# returns an ICollection containing all the values in the Hashtable. This allows you to iterate through or manipulate the values without accessing the keys directly.

Syntax

Following is the syntax to get the values collection from a Hashtable −

public virtual ICollection Values { get; }

Return Value

The Values property returns an ICollection containing all the values in the Hashtable. The order of values is not guaranteed since Hashtable does not maintain insertion order.

Using Hashtable.Values Property

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Hashtable hash = new Hashtable();
      hash.Add("A", "Electronics");
      hash.Add("B", "Appliances");
      hash.Add("C", "Pet Supplies");
      hash.Add("D", "Books");
      hash.Add("E", "Toys");
      hash.Add("F", "Footwear");
      hash.Add("G", "Clothing");
      
      ICollection values = hash.Values;
      Console.WriteLine("Values in the Hashtable:");
      foreach(string value in values)
         Console.WriteLine(value);
   }
}

The output of the above code is −

Values in the Hashtable:
Clothing
Electronics
Appliances
Pet Supplies
Books
Toys
Footwear

Using Values with Different Data Types

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Hashtable hash = new Hashtable();
      hash.Add(1, "AB");
      hash.Add(2, "CD");
      hash.Add(3, "EF");
      hash.Add(4, "GH");
      hash.Add(5, "IJ");
      hash.Add(6, "KL");
      hash.Add(7, "MN");
      
      ICollection values = hash.Values;
      Console.WriteLine("All values in the Hashtable:");
      foreach(string value in values)
         Console.WriteLine(value);
   }
}

The output of the above code is −

All values in the Hashtable:
MN
KL
IJ
GH
EF
CD
AB

Comparing Keys vs Values Collections

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Hashtable hash = new Hashtable();
      hash.Add("ID1", "Manager");
      hash.Add("ID2", "Developer");
      hash.Add("ID3", "Tester");
      
      ICollection keys = hash.Keys;
      ICollection values = hash.Values;
      
      Console.WriteLine("Keys collection:");
      foreach(string key in keys)
         Console.WriteLine(key);
         
      Console.WriteLine("\nValues collection:");
      foreach(string value in values)
         Console.WriteLine(value);
   }
}

The output of the above code is −

Keys collection:
ID3
ID1
ID2

Values collection:
Tester
Manager
Developer

Comparison

Property Returns Use Case
hash.Keys ICollection of all keys When you need to access or iterate through keys
hash.Values ICollection of all values When you need to access or iterate through values only

Conclusion

The Hashtable.Values property provides an efficient way to access all values in a Hashtable as an ICollection. This is particularly useful when you need to process or display values without requiring their corresponding keys, making your code more focused and efficient.

Updated on: 2026-03-17T07:04:36+05:30

142 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements