IDictionary<TKey,TValue>.Remove(TKey) 方法
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从 IDictionary<TKey,TValue> 中移除包含指定键的元素。
public:
bool Remove(TKey key);
C#
public bool Remove (TKey key);
abstract member Remove : 'Key -> bool
Public Function Remove (key As TKey) As Boolean
- key
- TKey
要移除的元素的键。
如果该元素已成功移除,则为 true
;否则为 false
。 如果在原始 false
中没有找到 key
,此方法也会返回 IDictionary<TKey,TValue>。
key
为 null
。
下面的代码示例演示如何使用 Remove 方法从字典中删除键/值对。
此代码是可以编译和执行的较大示例的一部分。 请参阅 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.");
}
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.");
}
' 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> 用于比较键的实现。
产品 | 版本 |
---|---|
.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 |