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

傳回

適用於