c# - How to update a key in a Dictionary

Update a key 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. 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. 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 key in a Dictionary. 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.

There is no direct way to update a key for a specific item in the Dictionary object. We have to apply some tricks to achieve this. At first, we create an empty Dictionary instance. Then we put all elements of the source Dictionary into the newly created Dictionary instance. While we put the source Dictionary items into the new Dictionary, we have to alter/update the item whose key we have to update.

We put the updated element into the temporary Dictionary with the new key instead of the source element key. So, we get a duplicate Dictionary object but our desired element key is updated. Finally, we assign the temporary Dictionary elements to the source Dictionary. This way the source Dictionary’s specified key is updated.
dictionary-update-key.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,"Australian Pelican"},
            {2,"Brown Pelican"},
            {3,"Pygmy Cormorant"}
        };

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

        //create a temporary dictionary
        Dictionary<int, string> temp = new Dictionary<int, string>();
        
        foreach (KeyValuePair<int,string> pair in birds)
        {
            //set ney key is old key plus 10
            int key = pair.Key + 10;
            temp.Add(key,pair.Value);
        }

        //assign temporary dictionary elements to old dictionary.
        birds = temp;

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