c# - How to get key by index from a Dictionary

Dictionary get key by index
.Net framework dictionary represents a collection of keys and values. each element of dictionary contain a key andvalue pair. so to get the dictionary key by index, first we need to find the specified element by index and then get thiselement's key.

Enumerable.ElementAt<TSource> method allow us to get the element at a specified index in a sequence. this method exists in System.Linqnamespace. this method type parameter is 'TSource' which represents type of the elements of 'source'. ElementAt() method require to pass twoparameters named 'source' and 'index'.

'source' parameter type is System.Collections.Generic.IEnumerable<TSource> which representsan IEnumerable<T> to return an element from. 'index' parameter value data type is System.Int32 which represents the zero based index of theelement to retrieve.

this method throw ArgumentNullException exception, if the 'source' is null. method also throw ArgumentOutOfRangeException exception, if the 'index' is less than zero or greater than or equal to the number of elements in 'source'.

Enumerable.ElementAt<TSource> method return value type is 'TSource' which represents the element at the specified index position of thesource sequence. so by using this method we can get an element of dictionary from a specified index. after retrieving the element we can get its'Key' by accessing Pair.Key. finally the process is Dictionary<TKey, TValue>.ElementAt(index).Key.

the following asp.net c# example code demonstrate us how can we get dictionary key by index programmatically at run timein an asp.net application.
dictionary-get-key-by-index.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>    
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        //initialize a dictionary with keys and values.
        Dictionary<int, string> birds = new Dictionary<int, string>() {
            {10,"Southern Lapwing"},
            {20,"Eurasian Golden Plover"},
            {30,"Grey Plover"},
            {40,"Ringed Plover"},
            {50,"Kentish Plover"}
        };

        Label1.Text = "dictionary elements with index..........";
        for (int i = 0; i < birds.Count; i++)
        {
            Label1.Text += "<br />" + "index: " + i;
            Label1.Text += " key: " + birds.ElementAt(i).Key;
            Label1.Text += " value: " + birds.ElementAt(i).Value;
        }

        //get key of dictionary element by index.
        int keyOfIndex1 = birds.ElementAt(1).Key;

        //get key of dictionary index 3 element.
        int keyOfIndex3 = birds.ElementAt(3).Key;

        Label1.Text += "<br /><br />key of dictionary element at index 1: " + keyOfIndex1;
        Label1.Text += "<br />key of dictionary element at index 3: " + keyOfIndex3;
    }    
</script>    

<html xmlns="http://www.w3.org/1999/xhtml">    
<head id="Head1" runat="server">    
    <title>c# example - dictionary get key by index</title>    
</head>    
<body>    
    <form id="form1" runat="server">    
    <div>    
        <h2 style="color:MidnightBlue; font-style:italic;">    
            c# example - dictionary get key by index
        </h2>    
        <hr width="550" align="left" color="Gainsboro" />    
        <asp:Label     
            ID="Label1"     
            runat="server"    
            Font-Size="Large"  
            >    
        </asp:Label>    
        <br /><br />  
        <asp:Button     
            ID="Button1"     
            runat="server"     
            Text="dictionary get key by index"    
            OnClick="Button1_Click"  
            Height="40"    
            Font-Bold="true"    
            />    
    </div>    
    </form>    
</body>    
</html>