c# - How to add a key value pair to a Dictionary

Dictionary add key value pair
The Dictionary class represents a collection of keys and values. .Net framework’s Dictionary is located under the System.Collections.Generic namespace. The Dictionary object constructor is Dictionary<TKey,TValue>. The TKey is the data type of the keys in the Dictionary and the TValue is the data type of the values in the Dictionary. We can initialize an empty Dictionary instance and add elements to it using its Add() method.

The following .net c# tutorial code demonstrates how we can add key-value pairs to a Dictionary object. Dictionary each item contains a key-value pair. So if we want to add the item to the dictionary collection we have to add a key-value pair to the Dictionary instance.

The .net framework’s KeyValuePair<TKey,TValue> defines a key-value pair that can be set and retrieved. Here the Tkey is the data type of key and TValue is the data type of value of the KeyValuePair instance. So we can create a KeyValuePair instance using its simple constructor.

The Dictionary class Add() method allows us to add a specified key and value of the Dictionary object. We can directly add an element to the Dictionary object using its Add() method. Or we can at first create a KeyValuePair object and add its key and value to the Dictionary using Add() method.

Dictionary Add(TKey key,TValue value) method has two arguments. The TKey is the key of the element to add and the TValue is the value of the element. The key can’t be null and it must be unique in the entire Dictionary elements. But the value can be null for reference types.

The Add() method throws ArgumentNullException if the element key is null. This method also generates ArgumentException if an element with the same key already exists in the Dictionary instance. So finally, we can add a key-value pair to the Dictionary object using its Add() method.
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>
More c# examples