Dictionary remove first element
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>
- How to iterate over a Dictionary using foreach loop
- How to initialize a Dictionary
- How to add a key value pair to a Dictionary
- How to sort a Dictionary by key
- How to sort a Dictionary by value
- How to sort a Dictionary in ascending and descending order
- How to sort a Dictionary by DateTime value
- How to remove items from Dictionary in foreach loop
- How to remove items from a Dictionary while iterating
- How to remove items from a Dictionary in loop