Dictionary remove range
dictionary-remove-range.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,"Musk Lorikeet"},
{2,"Papuan Lorikeet"},
{3,"Bluebonnet"},
{4,"Crimson Rosella"},
{5,"Mulga Parrot"},
{6,"Regent Parrot"}
};
Label1.Text = "dictionary keys and values..........";
foreach (KeyValuePair<int , string> pair in birds)
{
Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
}
int rangeStart = 2;
int rangeEnd = 4;
List<int> keysList = new List<int>();
for(int i=rangeStart;i<=rangeEnd;i++)
{
//zero based index on dictionary.
int keysToRemove = birds.Keys.ElementAt(i);
keysList.Add(keysToRemove);
}
Label1.Text += "<br /><br />removing keys from dictionary index range 2 to 4<br />";
foreach (int key in keysList)
{
//remove element from dictionary.
birds.Remove(key);
Label1.Text += "key [" + key + "] removed from dictionary<br />";
}
Label1.Text += "<br />dictionary after removing elements index range 2 to 4 ..........";
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 range</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - dictionary remove range
</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 range"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>
- How to remove first element from Dictionary
- How to remove items from Dictionary in foreach loop
- How to get a range of items from a Dictionary
- How to remove items from a Dictionary by condition
- How to remove an item from Dictionary by value
- How to access Dictionary items by index
- How to get index of an item by key from a Dictionary
- How to update a key in a Dictionary
- How to access Dictionary items
- How to convert a Dictionary to a string