C# C# Console Application

How to convert Dictionary keys into Array in C#1 min read

.Net framework Dictionary<TKey, TValue> class represents a collection of keys and values. the Dictionary<TKey, TValue>.Keys property allow us to get a collection containing the keys in the Dictionary<TKey, TValue>. this Keys property value type is System.Collections.Generic.Dictionary<TKey, TValue>.KeyCollection.

to convert a dictionary keys to array, first we need to get the dictionary keys by its ‘Keys’ property. this property return a collection (generic list) of dictionary keys. after getting the List<T>, we can convert it to an array by List class ToArray() method.




List<T>.ToArray() method allow us to copy the elements of the List<T> to a new array. so we can convert the dictionary keys into array as Dictionary<TKey, TValue>.Keys.ToArray().

the following  c# example code demonstrate us how can we convert dictionary keys to an array programmatically at run time in an application.

C# Code:

Output:

Leave a Comment