Get key value pair from dictionary
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>
- How to get key by index from a Dictionary
- How to perform foreach loop through Dictionary keys
- How to get first key value from Dictionary
- How to merge two Dictionaries
- How to union two Dictionaries
- How to update a key in a Dictionary
- How to access Dictionary items
- How to create a Dictionary from a list
- How to reverse a Dictionary items
- How to convert a Dictionary to a string