Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
SortedDictionary.Values Property in C#
The SortedDictionary.Values property in C# is used to get a collection containing the values in the SortedDictionary<TKey, TValue>. This property returns a ValueCollection that preserves the sorted order of the original dictionary based on the keys.
Syntax
Following is the syntax for the Values property −
public SortedDictionary<TKey,TValue>.ValueCollection Values { get; }
Return Value
The property returns a SortedDictionary<TKey,TValue>.ValueCollection containing all the values in the sorted dictionary. The values maintain the same order as their corresponding keys in the sorted dictionary.
Using Values Property to Iterate Through Values
The following example demonstrates how to use the Values property to get and iterate through all values −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
sortedDict.Add(3, "Notebook");
sortedDict.Add(1, "Ultrabook");
sortedDict.Add(5, "Flash Drive");
sortedDict.Add(2, "Alienware");
sortedDict.Add(4, "Connector");
Console.WriteLine("SortedDictionary key-value pairs:");
foreach (var kvp in sortedDict) {
Console.WriteLine($"Key = {kvp.Key}, Value = {kvp.Value}");
}
SortedDictionary<int, string>.ValueCollection values = sortedDict.Values;
Console.WriteLine("\nValues (in sorted order by key):");
foreach (string value in values) {
Console.WriteLine(value);
}
}
}
The output of the above code is −
SortedDictionary key-value pairs: Key = 1, Value = Ultrabook Key = 2, Value = Alienware Key = 3, Value = Notebook Key = 4, Value = Connector Key = 5, Value = Flash Drive Values (in sorted order by key): Ultrabook Alienware Notebook Connector Flash Drive
Using Values Property with String Keys
This example shows the Values property working with string keys, which are sorted alphabetically −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedDictionary<string, int> priceDict = new SortedDictionary<string, int>();
priceDict.Add("Laptop", 80000);
priceDict.Add("Mouse", 1500);
priceDict.Add("Keyboard", 3000);
priceDict.Add("Monitor", 25000);
Console.WriteLine("Product prices (sorted by product name):");
foreach (var item in priceDict) {
Console.WriteLine($"{item.Key}: ${item.Value}");
}
var prices = priceDict.Values;
Console.WriteLine("\nPrice values only:");
foreach (int price in prices) {
Console.WriteLine($"${price}");
}
Console.WriteLine($"\nTotal products: {prices.Count}");
}
}
The output of the above code is −
Product prices (sorted by product name): Keyboard: $3000 Laptop: $80000 Monitor: $25000 Mouse: $1500 Price values only: $3000 $80000 $25000 $1500 Total products: 4
Key Points
-
The
Valuesproperty returns a live view of the dictionary values. If the dictionary changes, the collection changes too. -
Values are returned in the same order as their corresponding keys in the sorted dictionary.
-
The returned
ValueCollectionis read-only and cannot be modified directly. -
Time complexity for accessing the
Valuesproperty is O(1), but iterating through all values takes O(n) time.
Conclusion
The SortedDictionary.Values property provides an efficient way to access all values in a sorted dictionary while maintaining the sorted order based on keys. This property is particularly useful when you need to iterate through values or perform operations on all values without accessing the keys.
