Dictionary<TKey,TValue>.Remove Dictionary<TKey,TValue>.Remove Dictionary<TKey,TValue>.Remove Dictionary<TKey,TValue>.Remove Method

定义

重载

Remove(TKey) Remove(TKey) Remove(TKey) Remove(TKey)

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

Remove(TKey, TValue) Remove(TKey, TValue) Remove(TKey, TValue) Remove(TKey, TValue)

Remove(TKey) Remove(TKey) Remove(TKey) 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 TKey TKey TKey

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

返回

如果成功找到并移除该元素,则为 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>.

实现

异常

示例

下面的代码示例演示如何使用Remove方法从字典中移除键/值对。The 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.

另请参阅

Remove(TKey, TValue) Remove(TKey, TValue) Remove(TKey, TValue) Remove(TKey, TValue)

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 TKey TKey TKey
value
TValue TValue TValue TValue

返回

适用于