Convert Dictionary values to an Array in C#
This post will discuss how to convert Dictionary<TKey,TValue> values to an Array in C#.
1. Using Dictionary<TKey,TValue>.Values Property
The Dictionary<TKey,TValue>.Values property to returns a collection containing dictionary’s values. The idea is to allocate an array to accommodate all the values of the dictionary, and then use the CopyTo() method to copy values from the dictionary to the array. The following code example demonstrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.Collections.Generic; public class Example { public static void Main() { Dictionary<string, int> dict = new Dictionary<string, int>() { {"A", 1}, {"B", 2}, {"C", 3}, {"D", 4}, {"E", 5} }; int[] values = new int[dict.Count]; dict.Values.CopyTo(values, 0); Console.WriteLine(String.Join(", ", values)); // 1, 2, 3, 4, 5 } } |
Alternatively, you can use the ToArray() method to convert the collection returned by the Value property into an array. Note that the ToArray() method requires LINQ and you will need to include System.Linq namespace.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { Dictionary<string, int> dict = new Dictionary<string, int>() { {"A", 1}, {"B", 2}, {"C", 3}, {"D", 4}, {"E", 5} }; var values = dict.Values.ToArray(); Console.WriteLine(String.Join(", ", values)); // 1, 2, 3, 4, 5 } } |
If you are not allowed to use LINQ, you can store values in a List<T> using its constructor.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.Collections.Generic; public class Example { public static void Main() { Dictionary<string, int> dict = new Dictionary<string, int>() { {"A", 1}, {"B", 2}, {"C", 3}, {"D", 4}, {"E", 5} }; List<int> values = new List<int>(dict.Values); Console.WriteLine(String.Join(", ", values)); // 1, 2, 3, 4, 5 } } |
If you particularly need an array, you can call the ToArray() method on the list. However, this creates an intermediate list object and should be avoided.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.Collections.Generic; public class Example { public static void Main() { Dictionary<string, int> dict = new Dictionary<string, int>() { {"A", 1}, {"B", 2}, {"C", 3}, {"D", 4}, {"E", 5} }; int[] values = new List<int>(dict.Values).ToArray(); Console.WriteLine(String.Join(", ", values)); // 1, 2, 3, 4, 5 } } |
2. Using Enumerable.Select() Method
The Select() method from LINQ transforms each element of a sequence into a new form. The following code example demonstrates how we can use Select() to project over elements of a dictionary and get an array of its values.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { Dictionary<string, int> dict = new Dictionary<string, int>() { {"A", 1}, {"B", 2}, {"C", 3}, {"D", 4}, {"E", 5} }; int[] values = dict.Select(x => x.Value).ToArray(); Console.WriteLine(String.Join(", ", values)); // 1, 2, 3, 4, 5 } } |
The Select() method can be used to transform the dictionary into an array of strings, containing key-value pairs, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { Dictionary<string, int> dict = new Dictionary<string, int>() { {"A", 1}, {"B", 2}, {"C", 3}, {"D", 4}, {"E", 5} }; string[] array = dict.Select(item => string.Format("[{0}, {1}]", item.Key, item.Value)) .ToArray(); Console.WriteLine(String.Join(", ", array)); // [A, 1], [B, 2], [C, 3], [D, 4], [E, 5] } } |
That’s all about converting Dictionary<TKey,TValue> values to an Array in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)