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.

<%@ 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.

        //alternate way to remove dictionary first element.

        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;
<html xmlns="http://www.w3.org/1999/xhtml">    
<head id="Head1" runat="server">    
    <title>c# example - dictionary remove first element</title>    
    <form id="form1" runat="server">    
        <h2 style="color:MidnightBlue; font-style:italic;">    
            c# example - dictionary remove first element
        <hr width="550" align="left" color="Gainsboro" />    
        <br /><br />  
            Text="dictionary remove first element"    
More c# examples