IDictionary<TKey,TValue>.Remove(TKey) IDictionary<TKey,TValue>.Remove(TKey) IDictionary<TKey,TValue>.Remove(TKey) IDictionary<TKey,TValue>.Remove(TKey) Method

定义

IDictionary<TKey,TValue> 中移除包含指定键的元素。Removes the element with the specified key from the IDictionary<TKey,TValue>.

public:
 bool Remove(TKey key);
public bool Remove (TKey key);
abstract member Remove : 'Key -> bool
Public Function Remove (key As TKey) As Boolean

参数

key
TKey TKey TKey TKey

要移除的元素的键。The key of the element to remove.

返回

如果该元素已成功移除,则为 true;否则为 falsetrue if the element is successfully removed; otherwise, false. 如果在原始 IDictionary<TKey,TValue> 中没有找到 key,此方法也会返回 falseThis method also returns false if key was not found in the original IDictionary<TKey,TValue>.

异常

示例

下面的代码示例演示如何使用Remove方法从字典中移除键/值对。The following code example shows how to remove a key/value pair from a dictionary using the Remove method.

此代码是可以编译和执行的更大示例的一部分。This code is part of a larger example that can be compiled and executed. 请参阅 System.Collections.Generic.IDictionary<TKey,TValue>See System.Collections.Generic.IDictionary<TKey,TValue>.

// Use the Remove method to remove a key/value pair.
Console::WriteLine("\nRemove(\"doc\")");
openWith->Remove("doc");

if (!openWith->ContainsKey("doc"))
{
    Console::WriteLine("Key \"doc\" is not found.");
}
// Use the Remove method to remove a key/value pair.
Console.WriteLine("\nRemove(\"doc\")");
openWith.Remove("doc");

if (!openWith.ContainsKey("doc"))
{
    Console.WriteLine("Key \"doc\" is not found.");
}
' Use the Remove method to remove a key/value pair.
Console.WriteLine(vbLf + "Remove(""doc"")")
openWith.Remove("doc")

If Not openWith.ContainsKey("doc") Then
    Console.WriteLine("Key ""doc"" is not found.")
End If

注解

实现在确定对象的相等性方面可能有所不同;例如, List<T>类使用Comparer<T>.Default,而Dictionary<TKey,TValue>类允许用户指定IComparer<T>要用于比较键的实现。Implementations can vary in how they determine equality of objects; for example, the List<T> class uses Comparer<T>.Default, whereas the Dictionary<TKey,TValue> class allows the user to specify the IComparer<T> implementation to use for comparing keys.

适用于

另请参阅