Get an ICollection containing the values in HybridDictionary in C#

The HybridDictionary class in C# provides the Values property that returns an ICollection containing all the values in the dictionary. This collection can be used to iterate through values or copy them to an array for further processing.

Syntax

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

ICollection values = hybridDictionary.Values;

To copy values to an array −

hybridDictionary.Values.CopyTo(array, startIndex);

How It Works

The Values property returns an ICollection that contains all the values stored in the HybridDictionary. The order of values may vary depending on the internal structure used by HybridDictionary - it uses a ListDictionary for small collections and switches to a Hashtable for larger ones.

HybridDictionary Values Collection HybridDictionary "One" ? "Katie" "Two" ? "Andy" ... .Values ICollection "Katie" "Andy" ... Values collection contains only the values, not the keys from the dictionary

Using Values Property with CopyTo Method

Example

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main(){
      HybridDictionary dict = new HybridDictionary();
      dict.Add("One", "Katie");
      dict.Add("Two", "Andy");
      dict.Add("Three", "Gary");
      dict.Add("Four", "Mark");
      dict.Add("Five", "Marie");
      dict.Add("Six", "Sam");
      dict.Add("Seven", "Harry");
      dict.Add("Eight", "Kevin");
      dict.Add("Nine", "Ryan");
      
      String[] strArr = new String[dict.Count];
      dict.Values.CopyTo(strArr, 0);
      
      Console.WriteLine("Values in HybridDictionary:");
      for (int i = 0; i < dict.Count; i++)
         Console.WriteLine(strArr[i]);
   }
}

The output of the above code is −

Values in HybridDictionary:
Kevin
Harry
Mark
Gary
Katie
Marie
Sam
Andy
Ryan

Using Values Property with Foreach Loop

Example

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      HybridDictionary dict = new HybridDictionary();
      dict.Add("1", "A");
      dict.Add("2", "B");
      dict.Add("3", "C");
      dict.Add("4", "D");
      dict.Add("5", "E");
      dict.Add("6", "F");
      
      Console.WriteLine("Iterating through values:");
      foreach (string value in dict.Values) {
         Console.WriteLine(value);
      }
      
      Console.WriteLine("\nTotal values count: " + dict.Values.Count);
   }
}

The output of the above code is −

Iterating through values:
A
B
C
D
E
F

Total values count: 6

Common Use Cases

The Values property is commonly used when you need to −

  • Extract all values from the dictionary without their corresponding keys

  • Convert dictionary values to an array for further processing

  • Iterate through all values using foreach loops

  • Check if a specific value exists in the dictionary

Conclusion

The Values property of HybridDictionary returns an ICollection containing all dictionary values. This collection can be iterated directly or copied to an array using the CopyTo method, making it useful for extracting and processing dictionary values independently of their keys.

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

159 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements