Dictionary.Values Property in C#

The Dictionary.Values property in C# is used to retrieve all the values stored in a Dictionary<TKey, TValue>. This property returns a ValueCollection that contains all values from the dictionary, preserving the order in which they were added.

Syntax

Following is the syntax for the Dictionary.Values property −

public Dictionary<TKey, TValue>.ValueCollection Values { get; }

Return Value

The property returns a Dictionary<TKey, TValue>.ValueCollection containing all the values in the dictionary. This collection is a live view of the dictionary values, meaning changes to the dictionary are reflected in the collection.

Dictionary.Values Property Dictionary "One" ? "Smith" "Two" ? "Jones" "Three" ? "Brown" "Four" ? "Davis" Key-Value Pairs .Values ValueCollection "Smith" "Jones" "Brown" "Davis" Values only, no keys

Using Dictionary.Values with String Keys

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      Dictionary<string, string> dict = new Dictionary<string, string>();
      dict.Add("One", "Kagido");
      dict.Add("Two", "Ngidi");
      dict.Add("Three", "Devillers");
      dict.Add("Four", "Smith");
      dict.Add("Five", "Warner");
      
      Console.WriteLine("Count of elements = " + dict.Count);
      Console.WriteLine("\nKey/value pairs...");
      
      foreach(KeyValuePair<string, string> res in dict){
         Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
      }
      
      Console.WriteLine("\nAll the values...");
      Dictionary<string, string>.ValueCollection allValues = dict.Values;
      
      foreach(string str in allValues){
         Console.WriteLine("Value = {0}", str);
      }
   }
}

The output of the above code is −

Count of elements = 5

Key/value pairs...
Key = One, Value = Kagido
Key = Two, Value = Ngidi
Key = Three, Value = Devillers
Key = Four, Value = Smith
Key = Five, Value = Warner

All the values..
Value = Kagido
Value = Ngidi
Value = Devillers
Value = Smith
Value = Warner

Using Dictionary.Values with Integer Keys

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      Dictionary<int, string> dict = new Dictionary<int, string>();
      dict.Add(1, "Kagido");
      dict.Add(2, "Ngidi");
      dict.Add(3, "Devillers");
      
      Console.WriteLine("Count of elements = " + dict.Count);
      Console.WriteLine("\nKey/value pairs...");
      
      foreach(KeyValuePair<int, string> res in dict){
         Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
      }
      
      Console.WriteLine("\nAll the values...");
      Dictionary<int, string>.ValueCollection allValues = dict.Values;
      
      foreach(string str in allValues){
         Console.WriteLine("Value = {0}", str);
      }
   }
}

The output of the above code is −

Count of elements = 3

Key/value pairs...
Key = 1, Value = Kagido
Key = 2, Value = Ngidi
Key = 3, Value = Devillers

All the values..
Value = Kagido
Value = Ngidi
Value = Devillers

Common Use Cases

  • Data Processing: When you need to perform operations on all values without caring about their keys.

  • Validation: Checking if all values meet certain criteria or finding duplicates.

  • Export Operations: Converting dictionary values to arrays or lists for further processing.

  • Statistical Analysis: Computing aggregates like sum, average, or count of dictionary values.

Conclusion

The Dictionary.Values property provides an efficient way to access all values in a dictionary without needing to iterate through key-value pairs. It returns a live collection that reflects changes made to the original dictionary, making it useful for data processing and analysis scenarios.

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

366 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements