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

Dictionary get item index by key
The Dictionary class represents a collection of keys and values. .Net framework’s Dictionary is located under the System.Collections.Generic namespace. The Dictionary object constructor is Dictionary<TKey,TValue>. The TKey is the data type of the keys in the Dictionary and the TValue is the data type of the values in the Dictionary. We can initialize an empty Dictionary instance and add elements to it using its Add() method.

The following .net c# tutorial code demonstrates how we can get an item index by its key from a Dictionary object. Each Dictionary item is built with a key-value pair. Dictionary keys are unique but values can be duplicated. So we can easily identify an item from a Dictionary using the specified item key.

This way, we can get the Dictionary item’s index position using its item key. To achieve this, at first, we converted the Dictionary keys to a list, then we get the specified key’s index position from this keys list.

The Enumerable ToList() method creates a List<T> from an IEnumerable<T>. The IndexOf() method determines the index of a specific item in the list.

Another way to get an item index from Dictionary items is that, loop through the items collection of the specified Dictionary. At the looping time, we match the specified key with each item, and when we get the item we also get the index of the item. The Enumerable ElementAt() method returns the element at a specified index in a sequence.

<%@ 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>() {
            {1,"Banded Stilt"},
            {2,"Pied Avocet"},
            {3,"Blacksmith Plover"},
            {4,"Northern Lapwing"},
            {5,"Masked Lapwing"}

        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;
        //find index of dictionary elements by key.
        int index = birds.Keys.ToList().IndexOf(3);

        Label1.Text += "<br /><br />index of key [3] is: " + index;
        //another way to find index of dictionary elements by key.
        for (int i = 0; i < birds.Count;i++ )
            if (birds.ElementAt(i).Key == 4)
                Label1.Text += "<br />index of key [4] is: " + i;
<html xmlns="http://www.w3.org/1999/xhtml">    
<head id="Head1" runat="server">    
    <title>c# example - dictionary index of key</title>    
    <form id="form1" runat="server">    
        <h2 style="color:MidnightBlue; font-style:italic;">    
            c# example - dictionary index of key
        <hr width="550" align="left" color="Gainsboro" />    
        <br /><br />  
            Text="dictionary index of key"    
More c# examples