c# - How to get a key value pair from Dictionary

Get a key value pair from Dictionary
The Dictionary class represents a collection of keys and values. The .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 a key-value pair from a Dictionary at a specified index position. Dictionary each item is consist of a key-value pair. So to get an item from a Dictionary also means to get a key-value pair from a Dictionary items collection.

We know that in the .net framework, we can get an element from a collection very easily. The .net c# developers can get a specified element from a collection using its index position.

The Enumerable ElementAt() method returns the element at a specified index in a sequence. And the Enumerable ElementAtOrDefault() method returns the element at a specified index in a sequence or a default value if the index is out of range. So this ElementAtOrDefault() method is safer than ElementAt() method.

In the following example .net c# code, we used both ElementAt() and ElementAtOrDefault() methods to get a specified element from a Dictionary items collection. We get the item from a Dictionary at a specified index position.
get-key-value-pair-from-dictionary.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,"Parasitic Skua"},
            {20,"Great Skua"},
            {30,"Atlantic Puffin"},
            {40,"Little Auk"},
            {50,"Guillemot"}
        };

        Label1.Text = "dictionary keys and values..........";
        foreach (KeyValuePair<int, string> pair in birds)
        {
            Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
        }

        //get keyvaluepair from dictionary at index 1.
        KeyValuePair<int, string> pairOfIndex1 = birds.ElementAt(1);

        //get keyvaluepair from dictionary at index 3.
        KeyValuePair<int, string> pairOfIndex3 = birds.ElementAtOrDefault(3);

        Label1.Text += "<br /><br />dictionary KeyValuePair at index 1......<br />";
        Label1.Text += pairOfIndex1.Key + " ........ " + pairOfIndex1.Value;

        Label1.Text += "<br /><br />dictionary KeyValuePair at index 3......<br />";
        Label1.Text += pairOfIndex3.Key + " ........ " + pairOfIndex3.Value;
    }    
</script>    
    
<html xmlns="http://www.w3.org/1999/xhtml">    
<head id="Head1" runat="server">    
    <title>c# example - dictionary find key by value</title>    
</head>    
<body>    
    <form id="form1" runat="server">    
    <div>    
        <h2 style="color:MidnightBlue; font-style:italic;">    
            c# example - get key value pair from dictionary
        </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="get key value pair from dictionary"    
            OnClick="Button1_Click"  
            Height="40"    
            Font-Bold="true"    
            />    
    </div>    
    </form>    
</body>    
</html>
More c# examples