Dictionary Remove() Method
.Net framework dictionary Remove() method allow us to remove the value with the specified key from the
Dictionary<TKey, TValue>. this Dictionary<TKey, TValue>.Remove() method exists under System.Collections.Generic
namespace. this method require to pass a parameter named 'key'.
the 'key' parameter value type is 'TKey' which represents the key of the element to remove. the Remove() method retrun value data type is System.Boolean. this method return 'true', if the element is successfully found and removed; otherwise this method return 'false'. this method also return 'false', if 'key' is not found in dictionary.
the Remove() method implements as IDictionary<TKey, TValue>.Remove(TKey). this method throw ArgumentNullException exception, if the 'key' is null. if the specified 'key' is not found in dictionary then the dictionary remain unchanged and no exception is thrown.
the following asp.net c# example code demonstrate us how can we remove dictionary element by key programmatically at run time in an asp.net application.
the 'key' parameter value type is 'TKey' which represents the key of the element to remove. the Remove() method retrun value data type is System.Boolean. this method return 'true', if the element is successfully found and removed; otherwise this method return 'false'. this method also return 'false', if 'key' is not found in dictionary.
the Remove() method implements as IDictionary<TKey, TValue>.Remove(TKey). this method throw ArgumentNullException exception, if the 'key' is null. if the specified 'key' is not found in dictionary then the dictionary remain unchanged and no exception is thrown.
the following asp.net c# example code demonstrate us how can we remove dictionary element by key programmatically at run time in an asp.net application.
DictionaryRemoveMethod.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
Dictionary<string, string> colors = new Dictionary<string, string>();
colors.Add("PaleVioletRed", "#D87093");
colors.Add("PaleGreen", "#98FB98");
colors.Add("Purple", "#800080");
colors.Add("SteelBlue", "#4682B4");
Label1.Text = "Dictionary Keys...<br />";
foreach (string color in colors.Keys)
{
Label1.Text += color + "<br />";
}
//Remove() method remove the value with the specified key from the Dictionary.
colors.Remove("Purple");
Label1.Text += "<br />After Call Remove() Method With Key 'Purple'. Dictionary Keys...<br />";
foreach (string color in colors.Keys)
{
Label1.Text += color + "<br />";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Dictionary Remove() Method - How to remove the value with the specified key from Dictionary</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy; font-style:italic;">
System.Collections.Generic.Dictionary Remove() Method
<br /> How to remove the value with the specified key from Dictionary
</h2>
<hr width="600" align="left" color="Navy" />
<br />
<asp:Label
ID="Label1"
runat="server"
ForeColor="Purple"
Font-Names="Courier New"
Font-Size="Large"
>
</asp:Label>
<br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Test Dictionary Remove() Method"
Height="45"
Font-Bold="true"
ForeColor="DodgerBlue"
/>
</div>
</form>
</body>
</html>

- How to convert Dictionary keys into array
- How to convert Dictionary values into array
- How to count Dictionary items
- How to sort a Dictionary by key and value
- How to convert a Dictionary to a list
- How to merge two Dictionaries
- How to union two Dictionaries
- How to add an item to a Dictionary if it does not exists
- How to update a value in a Dictionary
- How to update a key in a Dictionary