What is the Values property of Hashtable class in C#?

The Values property of the Hashtable class in C# gets an ICollection containing all the values stored in the Hashtable. This property provides a way to access all values without needing to know their corresponding keys.

Syntax

Following is the syntax for using the Values property −

public virtual ICollection Values { get; }

To iterate through the values −

foreach (object value in hashtable.Values) {
    // process each value
}

Return Value

The Values property returns an ICollection object that contains all the values in the Hashtable. The collection is read-only and reflects the current state of the Hashtable.

Using Values Property to Display All Values

Example

using System;
using System.Collections;

class Program {
    static void Main(string[] args) {
        Hashtable ht = new Hashtable();

        ht.Add("One", "Henry");
        ht.Add("Two", "Kevin");
        ht.Add("Three", "David");

        Console.WriteLine("Values in the Hashtable:");
        foreach (string value in ht.Values) {
            Console.WriteLine(value);
        }
    }
}

The output of the above code is −

Values in the Hashtable:
David
Henry
Kevin

Using Values Property with Different Data Types

Example

using System;
using System.Collections;

class Program {
    static void Main(string[] args) {
        Hashtable ht = new Hashtable();

        ht.Add("ID1", 101);
        ht.Add("ID2", 202);
        ht.Add("ID3", 303);
        ht.Add("Name", "John");

        Console.WriteLine("All values in the Hashtable:");
        foreach (object value in ht.Values) {
            Console.WriteLine($"Value: {value}, Type: {value.GetType().Name}");
        }

        Console.WriteLine($"\nTotal number of values: {ht.Values.Count}");
    }
}

The output of the above code is −

All values in the Hashtable:
Value: John, Type: String
Value: 101, Type: Int32
Value: 202, Type: Int32
Value: 303, Type: Int32

Total number of values: 4

Converting Values to Array

Example

using System;
using System.Collections;

class Program {
    static void Main(string[] args) {
        Hashtable ht = new Hashtable();

        ht.Add("A", "Apple");
        ht.Add("B", "Banana");
        ht.Add("C", "Cherry");

        // Convert values to array
        object[] valuesArray = new object[ht.Values.Count];
        ht.Values.CopyTo(valuesArray, 0);

        Console.WriteLine("Values converted to array:");
        for (int i = 0; i < valuesArray.Length; i++) {
            Console.WriteLine($"Index {i}: {valuesArray[i]}");
        }
    }
}

The output of the above code is −

Values converted to array:
Index 0: Cherry
Index 1: Apple
Index 2: Banana

Key Points

  • The Values property returns a read-only collection ? you cannot modify the Hashtable through this collection.

  • The order of values is not guaranteed since Hashtable does not maintain insertion order.

  • The collection returned by Values property reflects live changes ? if you modify the Hashtable, the Values collection automatically reflects those changes.

  • You can use the Count property to get the total number of values.

Conclusion

The Values property of the Hashtable class provides convenient access to all values stored in the collection. It returns an ICollection that can be iterated, converted to arrays, or used to get the count of values, making it useful for various data processing scenarios.

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

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements