Dictionary<TKey,TValue>.Remove 方法

定義

多載

Remove(TKey)

將具有指定索引鍵的值從 Dictionary<TKey,TValue> 中移除。Removes the value with the specified key from the Dictionary<TKey,TValue>.

Remove(TKey, TValue)

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.

傳回

如果成功找到並移除項目則為 true,否則為 falsetrue if the element is successfully found and removed; otherwise, false. 如果在 false 中找不到 key,則這個方法會傳回 Dictionary<TKey,TValue>This method returns false if key is not found in the Dictionary<TKey,TValue>.

實作

例外狀況

keynullkey is null.

範例

下列程式碼範例顯示如何使用 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.

僅限 .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)

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

傳回

適用於