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
Get an ICollection containing the values in OrderedDictionary in C#
The OrderedDictionary class in C# provides a Values property that returns an ICollection containing all the values in the dictionary. This collection maintains the same insertion order as the original dictionary, making it useful when you need to work with dictionary values while preserving their order.
Syntax
Following is the syntax to access the Values property −
ICollection values = orderedDictionary.Values;
You can then iterate through the values or copy them to an array −
string[] valueArray = new string[orderedDictionary.Count]; values.CopyTo(valueArray, 0);
Using Values Property with CopyTo Method
The most common approach is to use the CopyTo method to transfer values from the ICollection to an array for easier manipulation −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
OrderedDictionary dict = new OrderedDictionary();
dict.Add("1", "One");
dict.Add("2", "Two");
dict.Add("3", "Three");
dict.Add("4", "Four");
dict.Add("5", "Five");
dict.Add("6", "Six");
dict.Add("7", "Seven");
dict.Add("8", "Eight");
ICollection col = dict.Values;
String[] strVal = new String[dict.Count];
col.CopyTo(strVal, 0);
Console.WriteLine("Displaying the values...");
for (int i = 0; i
The output of the above code is −
Displaying the values...
One
Two
Three
Four
Five
Six
Seven
Eight
Direct Iteration Through Values Collection
You can also iterate directly through the ICollection without copying to an array −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
OrderedDictionary dict = new OrderedDictionary();
dict.Add("One", "John");
dict.Add("Two", "Tim");
dict.Add("Three", "Katie");
dict.Add("Four", "Andy");
dict.Add("Five", "Gary");
dict.Add("Six", "Amy");
ICollection col = dict.Values;
Console.WriteLine("Displaying the Values...");
foreach (string value in col) {
Console.WriteLine(value);
}
}
}
The output of the above code is −
Displaying the Values...
John
Tim
Katie
Andy
Gary
Amy
Working with Mixed Data Types
Since OrderedDictionary stores values as object type, you can work with mixed data types −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
OrderedDictionary dict = new OrderedDictionary();
dict.Add("name", "Alice");
dict.Add("age", 25);
dict.Add("active", true);
dict.Add("salary", 50000.50);
ICollection values = dict.Values;
Console.WriteLine("Mixed data type values:");
foreach (object value in values) {
Console.WriteLine($"{value} ({value.GetType().Name})");
}
}
}
The output of the above code is −
Mixed data type values:
Alice (String)
25 (Int32)
True (Boolean)
50000.5 (Double)
Conclusion
The Values property of OrderedDictionary returns an ICollection that maintains insertion order and provides flexible ways to access dictionary values. You can either copy values to an array using CopyTo method or iterate directly through the collection using foreach loops.
