Get an ICollection containing the values in ListDictionary in C#

The ListDictionary class in C# provides a Values property that returns an ICollection containing all the values stored in the dictionary. This is useful when you need to iterate through or manipulate only the values without accessing the keys.

Syntax

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

ICollection values = listDictionary.Values;

Return Value

The Values property returns an ICollection object that contains all the values in the ListDictionary. The collection maintains the same order as the original dictionary entries.

Using Values Property with Device Names

The following example demonstrates how to retrieve and display all values from a ListDictionary containing device information −

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main(){
      ListDictionary listDict = new ListDictionary();
      listDict.Add("1", "Laptop");
      listDict.Add("2", "Tablet");
      listDict.Add("3", "Desktop");
      listDict.Add("4", "Speaker");
      listDict.Add("5", "Earphone");
      listDict.Add("6", "Headphone");
      
      ICollection col = listDict.Values;
      Console.WriteLine("Device names in the ListDictionary:");
      foreach(String s in col){
         Console.WriteLine(s);
      }
   }
}

The output of the above code is −

Device names in the ListDictionary:
Laptop
Tablet
Desktop
Speaker
Earphone
Headphone

Using Values Property with Names

Here's another example showing how to extract values from a ListDictionary containing person names −

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main(){
      ListDictionary listDict = new ListDictionary();
      listDict.Add("A", "Tom");
      listDict.Add("B", "John");
      listDict.Add("C", "Kevin");
      listDict.Add("D", "Tim");
      
      ICollection col = listDict.Values;
      Console.WriteLine("Names in the ListDictionary:");
      Console.WriteLine("Total values: " + col.Count);
      
      foreach(String s in col){
         Console.WriteLine(s);
      }
   }
}

The output of the above code is −

Names in the ListDictionary:
Total values: 4
Tom
John
Kevin
Tim

Key Features

  • The Values property returns a live collection that reflects changes made to the original ListDictionary.

  • Values are returned in the same order as they were inserted into the ListDictionary.

  • The returned ICollection is read-only ? you cannot modify the dictionary through this collection.

  • Use the Count property to get the total number of values in the collection.

Conclusion

The Values property of ListDictionary provides an efficient way to access all values as an ICollection. This is particularly useful when you need to iterate through values without concerning yourself with the corresponding keys, making data processing and display operations more straightforward.

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

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements