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. 如果在 false 中找不到 key,則這個方法會傳回 Dictionary<TKey,TValue>This 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 *  -> bool
Public Function Remove (key As TKey, ByRef value As TValue) As Boolean

參數

key
TKey TKey TKey TKey
value
TValue TValue TValue TValue

傳回

適用於