c# - How to add an item to a Dictionary

Dictionary Add() Method
.Net framework Dictionary<TKey, TValue>.Add() method allow us to add the specified key and value to the dictionary. dictionary Add() method exists in System.Collections.Generic namespace. Add() method need to pass two parameters named 'key' and 'value'.

the 'key' parameter value type is 'TKey' which represents the key of the element to add. 'value' parameter value type is 'TValue' which represents the value of the element to add into dictionary. this value can be null for reference types. 'key' cannot be null. this method implements as IDictionary<TKey, TValue>.Add(TKey, TValue).

dictionary Add() method throw ArgumentNullException exception if the 'key' parameter value is null. this method throw ArgumentException exception, if an element with the same key already exists in Dictionary<TKey, TValue>.

the following asp.net c# example code demonstrate us how can we add a key and value pair to dictionary programmatically in an asp.net application.
DictionaryAddMethod.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Collections.Generic" %>

<!DOCTYPE html>
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Dictionary<string, string> colors = new Dictionary<string, string>();
        colors.Add("AliceBlue", "#F0F8FF");
        colors.Add("AntiqueWhite", "#FAEBD7");
        colors.Add("BlueViolet", "#8A2BE2");
        colors.Add("CadetBlue", "#5F9EA0");

        Label1.Text = "Dictionary Keys.....<br />";
        foreach (string color in colors.Keys)
        {
            Label1.Text += color + "<br />";
        }

        colors.Add("CornFlowerBlue", "#6495ED");
        Label1.Text += "<br />After Adding 'CornFlowerBlue'. Dictionary Keys.....<br />";

        foreach (string color in colors.Keys)
        {
            Label1.Text += color + "<br />";
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Dictionary Add() Method - How to add the specified key and value to the Dictionary</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy; font-style:italic;">
            System.Collections.Generic.Dictionary Add() Method
            <br /> How to add the specified key and value to the Dictionary
        </h2>
        <hr width="550" align="left" color="Navy" />
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="Orchid"
             Font-Names="Courier New"
             Font-Size="Large"
             >
        </asp:Label>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Test Dictionary Add() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More c# examples