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 and value pair. so to get the dictionary key by index, first we need to find the specified element by index and then get this element's key.

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

'source' parameter type is System.Collections.Generic.IEnumerable<TSource> which represents an IEnumerable<T> to return an element from. 'index' parameter value data type is System.Int32 which represents the zero based index of the element 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 the source 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 time in an asp.net application.

<%@ 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;
<html xmlns="http://www.w3.org/1999/xhtml">    
<head id="Head1" runat="server">    
    <title>c# example - dictionary get key by index</title>    
    <form id="form1" runat="server">    
        <h2 style="color:MidnightBlue; font-style:italic;">    
            c# example - dictionary get key by index
        <hr width="550" align="left" color="Gainsboro" />    
        <br /><br />  
            Text="dictionary get key by index"    
More c# examples