Dictionary for loop
dictionary-for-loop.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,"Himalayan Monal"},
{2,"Great Argus"},
{3,"Red Junglefowl"},
{4,"Green Junglefowl"},
{5,"Ceylon Junglefowl"}
};
Label1.Text = "dictionary elements using for loop..........";
//get dictionary elements using for loop
for(int i=0;i<birds.Count;i++)
{
//get dictionary element key using index value
Label1.Text += "<br />" + birds.ElementAt(i).Key;
//get dictionary element value using index position
Label1.Text += "......." + birds.ElementAt(i).Value;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - dictionary for loop</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - dictionary for loop
</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 for loop"
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 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 find duplicate values from a Dictionary
- How to create a Dictionary from a list
- How to reverse a Dictionary items
- How to convert a Dictionary to a string