Dictionary add key value pair
dictionary-add-key-value-pair.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
Dictionary<int, string> plants = new Dictionary<int, string>();
plants.Add(1, "Mahogany Birch");
plants.Add(2, "Silver Birch");
plants.Add(3, "Spice Birch");
Label1.Text = "dictionary keys values.....<br />";
foreach (KeyValuePair<int, string> p in plants)
{
Label1.Text += p.Key + " | " + p.Value + "<br />";
}
//this line create a new keyvaluepair;
KeyValuePair<int, string> pair = new KeyValuePair<int, string>(4, "Bay Laurel");
//this line new keyvaluepair to dictionary.
plants.Add(pair.Key,pair.Value);
Label1.Text += "<br />after adding new key value pair.....<br />";
foreach (KeyValuePair<int,string> p in plants)
{
Label1.Text += p.Key + " | " + p.Value + "<br />";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - dictionary add key value pair</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - dictionary add key value pair
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="X-Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="dictionary add key value pair"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>
- How to iterate over a Dictionary using foreach loop
- How to initialize a Dictionary
- How to sort a Dictionary
- How to use Dictionary TryGetValue
- How to make Dictionary TryGetValue case insensitive
- How to remove items from a Dictionary by condition
- How to remove an item from Dictionary by value
- How to convert Dictionary values to list
- How to convert Dictionary keys to list
- How to access Dictionary items by index