Foreach key in dictionary
foreach-key-in-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>() {
{11,"Eurasian Curlew"},
{22,"Whimbrel"},
{33,"Common Redshank"},
{44,"Wandering Tattler"},
{55,"Greater Yellowlegs"}
};
Label1.Text = "dictionary keys and values..........";
foreach (KeyValuePair<int, string> pair in birds)
{
Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
}
Label1.Text += "<br /><br />dictionary keys only..........";
//get only keys of dictionary.
foreach (int key in birds.Keys)
{
Label1.Text += "<br />" + key;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - foreach key in dictionary</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - foreach key in 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="foreach key in dictionary"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>
- How to remove range of items from a Dictionary
- How to get a range of items from a Dictionary
- How to remove items from a Dictionary by condition
- How to convert Dictionary values to list
- How to convert Dictionary keys to list
- How to get key by index from a Dictionary
- How to get a value by key from a Dictionary
- How to find a key by value in a Dictionary
- How to reverse a Dictionary items
- How to convert a Dictionary to a string