c# - How to remove first element from Dictionary

Remove first element from 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.

The following .net c# tutorial code demonstrates how we can remove the first element from a Dictionary. The Dictionary elements are the collection of key-value pairs. So this tutorial will demonstrate how we can remove the first key-value pair from a Dictionary object.

The Dictionary Remove() method removes the value with the specified key from the Dictionary<TKey,TValue>. The Remove() method has a parameter named ‘TKey key’, this is the key of the element to remove. So, if we pass the Dictionary first element key to the Remove() method then it will remove the first element from the Dictionary instance.

In this c# tutorial code, we converted the Dictionary keys to a list and get the first key from the list. This key is the first element key of the Dictionary. Now we pass this key to the Dictionary Remove() method that removes the first element from Dictionary. The Remove() method returns a Boolean. It returns true if the element is successfully found and removed; otherwise, it returns false if the key is not found in the Dictionary. The Dictionary Remove() method throws ArgumentNullException if the provided key is null.
dictionary-remove-first-element.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,"Tundra Swan"},
            {2,"Hawaiian Goose"},
            {3,"Egyptian Goose"},
            {4,"Orinoco Goose"},
            {5,"Andean Goose"}
        };

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

        //this line remove/delete dictionary first keyvaluepair.
        birds.Remove(birds.Keys.First());

        //alternate way to remove dictionary first element.
        //birds.Remove(birds.Keys.FirstOrDefault());

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