IDictionary<TKey,TValue>.Remove(TKey) IDictionary<TKey,TValue>.Remove(TKey) IDictionary<TKey,TValue>.Remove(TKey) IDictionary<TKey,TValue>.Remove(TKey) Method

定義

IDictionary<TKey,TValue> 中移除具有指定之索引鍵的項目。Removes the element with the specified key from the IDictionary<TKey,TValue>.

public:
 bool Remove(TKey key);
public bool Remove (TKey key);
abstract member 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 removed; otherwise, false. 如果在原始的 IDictionary<TKey,TValue> 中找不到 key,則這個方法也會傳回 falseThis method also returns false if key was not found in the original IDictionary<TKey,TValue>.

例外狀況

範例

下列程式碼範例示範如何移除的索引鍵/值組字典,使用Remove方法。The following code example shows how to remove a key/value pair from a dictionary using the Remove method.

此程式碼是大型的編譯和執行範例的一部分。This code is part of a larger example that can be compiled and executed. 請參閱 System.Collections.Generic.IDictionary<TKey,TValue>See System.Collections.Generic.IDictionary<TKey,TValue>.

// 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

備註

實作有所不同它們如何判定相等的物件;例如,List<T>類別會使用Comparer<T>.Default,而Dictionary<TKey,TValue>類別可讓使用者指定IComparer<T>實作来用於比較索引鍵。Implementations can vary in how they determine equality of objects; for example, the List<T> class uses Comparer<T>.Default, whereas the Dictionary<TKey,TValue> class allows the user to specify the IComparer<T> implementation to use for comparing keys.

適用於

另請參閱