Dictionary<TKey,TValue>.Remove 方法

定义

重载

Remove(TKey)

Dictionary<TKey,TValue> 中移除所指定的键的值。Removes the value with the specified key from the Dictionary<TKey,TValue>.

Remove(TKey, TValue)

Dictionary<TKey,TValue> 中删除具有指定键的值,并将元素复制到 value 参数。Removes the value with the specified key from the Dictionary<TKey,TValue>, and copies the element to the value parameter.

Remove(TKey)

Dictionary<TKey,TValue> 中移除所指定的键的值。Removes the value with the specified key from the Dictionary<TKey,TValue>.

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

参数

key
TKey

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

返回

Boolean

如果成功找到并移除该元素,则为 true;否则为 falsetrue if the element is successfully found and removed; otherwise, false. 如果在 Dictionary<TKey,TValue> 中没有找到 key,则此方法返回 falseThis method returns false if key is not found in the Dictionary<TKey,TValue>.

实现

例外

key 上声明的默认值为 nullkey is null.

示例

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

此代码示例是为类提供的更大示例的一部分 Dictionary<TKey,TValue> (openWith 是此示例中使用的字典的名称) 。This code example is part of a larger example provided for the Dictionary<TKey,TValue> class (openWith is the name of the Dictionary used in this example).

// 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

注解

如果不 Dictionary<TKey,TValue> 包含具有指定键的元素,则将 Dictionary<TKey,TValue> 保持不变。If the Dictionary<TKey,TValue> does not contain an element with the specified key, the Dictionary<TKey,TValue> remains unchanged. 不引发异常。No exception is thrown.

此方法使用 O (1) 操作。This method approaches an O(1) operation.

仅限 .NET Core 3.0 +:可以安全地调用此转变方法,而不会对实例上的活动枚举器无效 Dictionary<TKey,TValue>.NET Core 3.0+ only: this mutating method may be safely called without invalidating active enumerators on the Dictionary<TKey,TValue> instance. 这并不意味着线程安全。This does not imply thread safety.

另请参阅

适用于

Remove(TKey, TValue)

Dictionary<TKey,TValue> 中删除具有指定键的值,并将元素复制到 value 参数。Removes the value with the specified key from the Dictionary<TKey,TValue>, and copies the element to the value parameter.

public:
 bool Remove(TKey key, [Runtime::InteropServices::Out] TValue % value);
public bool Remove (TKey key, out TValue value);
member this.Remove : 'Key * 'Value -> bool
Public Function Remove (key As TKey, ByRef value As TValue) As Boolean

参数

key
TKey

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

value
TValue

已删除的元素。The removed element.

返回

Boolean

如果成功找到并移除该元素,则为 true;否则为 falsetrue if the element is successfully found and removed; otherwise, false.

例外

key 上声明的默认值为 nullkey is null.

适用于