Dictionary get value by key
dictionary-get-value-by-key.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>() {
{1,"Spotted Sandpiper"},
{2,"Egyptian Plover"},
{3,"Wandering Tattler"},
{4,"Collared Pratincole"},
{5,"Mew Gull"}
};
Label1.Text = "dictionary keys and values..........";
foreach (KeyValuePair<int, string> pair in birds)
{
Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
}
//find index of dictionary elements by key.
int index = birds.Keys.ToList().IndexOf(3);
//get dictionary element value by index
string value = birds.Values.ElementAt(index);
Label1.Text += "<br /><br />value of key [3] in dictionary..........<br />";
Label1.Text += value;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - dictionary get value by key</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - dictionary get value by key
</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 value by key"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>

- How to get key by index from a Dictionary
- How to perform foreach loop through Dictionary keys
- How to find a key by value in a Dictionary
- How to get a key value pair from Dictionary
- How to get first key value from Dictionary
- How to add an item to a Dictionary if it does not exists
- How to update a value in a Dictionary
- How to iterate through a Dictionary using for loop
- How to find duplicate values from a Dictionary
- How to convert a Dictionary to a string