Best way to iterate over a Dictionary
iterate-over-a-dictionary.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
Dictionary<string,int> dic = new Dictionary<string,int>();
dic.Add("Red",1);
dic.Add("Green",2);
dic.Add("Blue", 5);
dic.Add("Yellow", 20);
Label1.Text = "";
foreach(KeyValuePair<string,int> entry in dic)
{
Label1.Text += "Key: " + entry.Key + "<br />";
Label1.Text += "Value: " + entry.Value + "<br /><br />";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>c# example - best way to iterate over a Dictionary</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:DarkBlue; font-style:italic;">
c# example - best way to iterate over a Dictionary
</h2>
<hr width="550" align="left" color="LightBlue" />
<asp:Label ID="Label1" runat="server">
</asp:Label>
<br />
<asp:Button
ID="Button1"
runat="server"
Text="Iterate Over A Dictionary"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>

- How to use KeyValuePair in a Dictionary
- How to sort a Dictionary by key and value
- How to convert a Dictionary to a list
- How to add a key value pair to a Dictionary
- How to sort a Dictionary
- How to sort a Dictionary by value in descending order
- How to sort a Dictionary in descending order
- How to sort a Dictionary in ascending and descending order
- How to sort a Dictionary by DateTime value
- How to use Dictionary TryGetValue