c# - How to update a value in a Dictionary

Update a value in a dictionary
The Dictionary class represents a collection of keys and values. The .net framework’s Dictionary is located under the System.Collections.Generic namespace. We can initialize an empty Dictionary instance and add elements to it using its Add() method. We also can add some items to the Dictionary at the initializing time.

The following .net c# tutorial code demonstrates how we can update a value in a Dictionary. Actually, this tutorial is for, how we can update a value in a specified element. Here we will update a value for a specified key.

We know that the Dictionary element is consist of a key and value pair. The Dictionary keys are unique and we can’t set the same key for multiple items. But the Dictionary values can be duplicated and we can set the same value for the multiple items in a Dictionary.

Updating a key in a Dictionary is difficult but we can update a value very easily. We can get an element from Dictionary by passing its key. When we get the element we can simply assign a new value to the specified element.
dictionary-update-value.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>
  
<!DOCTYPE html>    
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        //initialize a dictionary with keys and values.
        Dictionary<int, string> birds = new Dictionary<int, string>() {
            {1,"Eurasian Bittern"},
            {2,"Little Bittern"},
            {3,"Green Heron"},
            {4,"Squacco Heron"},
            {5,"Cattle Egret"}
        };

        Label1.Text = "dictionary keys and values..........";
        foreach (KeyValuePair<int, string> pair in birds)
        {
            Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
        }

        //update dictionary element value which key is 2
        birds[2] = "Goliath Heron";

        //update dictionary element value which key is 5
        birds[5] = "Great White Pelican";

        Label1.Text += "<br /><br />dictionary elements after updating..........";
        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 update value</title>    
</head>    
<body>    
    <form id="form1" runat="server">    
    <div>    
        <h2 style="color:MidnightBlue; font-style:italic;">    
            c# example - dictionary update value
        </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 update value"    
            OnClick="Button1_Click"  
            Height="40"    
            Font-Bold="true"    
            />    
    </div>    
    </form>    
</body>    
</html>
More c# examples