Dictionary<TKey,TValue>.Remove 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| Remove(TKey) |
從 中移除指定鍵值 Dictionary<TKey,TValue>的值。 |
| Remove(TKey, TValue) |
從 中移除指定鍵的 Dictionary<TKey,TValue>值,並將元素複製到參數中 |
Remove(TKey)
從 中移除指定鍵值 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 未在 中,key此方法會回傳Dictionary<TKey,TValue>。
實作
例外狀況
key 是 null。
範例
以下程式碼範例展示了如何使用此 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)
從 中移除指定鍵的 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
例外狀況
key 是 null。