IDictionary<TKey,TValue>.Remove(TKey) 方法

定義

IDictionary<TKey,TValue> 中移除具有指定之索引鍵的項目。

C#
public bool Remove (TKey key);

參數

key
TKey

要移除的項目索引鍵。

傳回

如果成功移除項目,則為 true,否則為 false。 如果在原始的 false 中找不到 key,則這個方法也會傳回 IDictionary<TKey,TValue>

例外狀況

keynull

範例

下列程式代碼範例示範如何使用 方法,從字典 Remove 中移除索引鍵/值組。

此程式代碼是可編譯和執行之較大範例的一部分。 請參閱 System.Collections.Generic.IDictionary<TKey,TValue>

C#
// 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.");
}

備註

實作可能會因判斷物件的相等方式而有所不同;例如, List<T> 類別會使用 Comparer<T>.Default,而 Dictionary<TKey,TValue> 類別可讓使用者指定要 IComparer<T> 用於比較索引鍵的實作。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另請參閱