c# - How to convert Dictionary keys into array

How to convert Dictionary Keys into array
.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 asp.net c# example code demonstrate us how can we convert dictionary keys to an array programmatically at run time in an asp.net application.
ConvertDictionaryKeysIntoArray.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Collections.Generic" %>

<!DOCTYPE html>
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Dictionary<string, string> colors = new Dictionary<string, string>();
        colors.Add("Fuchsia", "#FF00FF");
        colors.Add("GoldenRod", "#DAA520");
        colors.Add("Lavender", "#E6E6FA");
        colors.Add("LightGreen", "#90EE90");

        Label1.Text = "Dictionary Keys | Values.....<br />";
        foreach(KeyValuePair<string,string> pair in colors)
        {
            Label1.Text += "Key= "+pair.Key +" | Value= "+ pair.Value + "<br />";
        }

        string[] colorKeyArray = colors.Keys.ToArray();
        Label1.Text += "<br />Color Array Items....";
        foreach (string color in colorKeyArray)
        {
            Label1.Text += "<br />" + color;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to convert Dictionary Keys into array</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Crimson; font-style:italic;">
            System.Collections.Generic.Dictionary
            <br /> How to convert Dictionary Keys into array
        </h2>
        <hr width="450" align="left" color="DeepPink" />
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="DodgerBlue"
             Font-Names="Courier New"
             Font-Size="X-Large"
             Font-Italic="true"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Convert Dictionary Keys into array"
            Height="45"
            Font-Bold="true"
            ForeColor="Navy"
            />
    </div>
    </form>
</body>
</html>
More c# examples