Dictionary initializer
dictionary-initializer.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
//initialize a new empty dictionary.
Dictionary<int, string> plants = new Dictionary<int, string>();
//add keyvaluepair to dictionary.
plants.Add(1, "Bermuda Cress");
plants.Add(2, "Bulbous Cress");
plants.Add(3, "Pale Corydalis");
//initialize a dictionary with values.
Dictionary<int, string> birds = new Dictionary<int, string>() {
{1,"Roseate Tern"},
{2,"Antarctic Tern"},
{3,"Black Skimmer"},
{4,"Atlantic Puffin"}
};
Label1.Text = "plants dictionary keys and values..........";
foreach (KeyValuePair<int, string> pair in plants)
{
Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
}
Label1.Text += "<br /><br />birds dictionary keys and values..........";
foreach (KeyValuePair<int, string> pair in birds)
{
Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - dictionary initializer</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - dictionary initializer
</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 initializer"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>

- How to use KeyValuePair in a Dictionary
- Best way to iterate over a Dictionary
- How to iterate over a Dictionary using foreach loop
- How to add a key value pair to a Dictionary
- How to sort a Dictionary
- How to merge two Dictionaries
- How to union two Dictionaries
- How to create a Dictionary from a list
- How to reverse a Dictionary items
- How to convert a Dictionary to a string