通过


Dictionary<TKey,TValue>.Remove 方法

定义

重载

名称 说明
Remove(TKey)

从中移除具有指定键 Dictionary<TKey,TValue>的值。

Remove(TKey, TValue)

从 中移除具有指定键 Dictionary<TKey,TValue>的值,并将元素复制到 value 参数。

Remove(TKey)

Source:
Dictionary.cs
Source:
Dictionary.cs
Source:
Dictionary.cs
Source:
Dictionary.cs
Source:
Dictionary.cs

从中移除具有指定键 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

要删除的元素的键。

返回

true 如果成功找到并删除了元素,则为 ;否则,为 false. 如果在 .. 中false找不到此方法,keyDictionary<TKey,TValue>则返回此方法。

实现

例外

keynull

示例

下面的代码示例演示如何使用 Remove 该方法从字典中删除键/值对。

此代码示例是为 Dictionary<TKey,TValue> 类提供的大型示例的一部分(openWith 此示例中使用的字典的名称)。

// 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.
printfn "\nRemove(\"doc\")"
openWith.Remove "doc" |> ignore

if openWith.ContainsKey "doc" |> not then
    printfn "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>保持不变。 不会引发异常。

此方法接近 O(1) 操作。

仅限 .NET Core 3.0+ :可以安全地调用此可变方法,而不会使实例上的 Dictionary<TKey,TValue> 活动枚举器失效。 这并不表示线程安全。

另请参阅

适用于

Remove(TKey, TValue)

Source:
Dictionary.cs
Source:
Dictionary.cs
Source:
Dictionary.cs
Source:
Dictionary.cs
Source:
Dictionary.cs

从 中移除具有指定键 Dictionary<TKey,TValue>的值,并将元素复制到 value 参数。

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

要删除的元素的键。

value
TValue

已删除的元素。

返回

true 如果成功找到并删除了元素,则为 ;否则,为 false.

例外

keynull

适用于