Dictionary.Remove 方法 (System.Collections.Generic)

您所在的位置:网站首页 mapremove方法 Dictionary.Remove 方法 (System.Collections.Generic)

Dictionary.Remove 方法 (System.Collections.Generic)

2024-01-26 20:50| 来源: 网络整理| 查看: 265

从 Dictionary 中移除所指定的键的值。

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

要移除的元素的键。

返回 Boolean

如果成功找到并移除该元素,则为 true;否则为 false。 如果在 Dictionary 中没有找到 key,则此方法返回 false。

实现 Remove(TKey) 例外 ArgumentNullException

key 为 null。

示例

下面的代码示例演示如何使用 Remove 方法从字典中删除键/值对。

此代码示例是为 Dictionary 类提供的更大示例的一部分, (openWith 是此示例中使用的字典的名称) 。

// 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如果 不包含具有指定键的元素,则 Dictionary 保持不变。 不会引发异常。

此方法接近 O (1) 操作。

仅限 .NET Core 3.0+:可以安全地调用此可变方法,而不会使实例上的 Dictionary 活动枚举器失效。 这并不意味着线程安全。

另请参阅 Add(TKey, TValue) Remove(TKey) 适用于


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3