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

定义

从 中移除具有指定键的 SortedDictionary<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

要删除的元素的键。

返回

true 如果已成功删除元素,则为 ;否则,为 false. 此方法还会返回找不到时

实现

例外

keynull

示例

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

该代码示例是 SortedDictionary<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(vbLf + "Remove(""doc"")")
openWith.Remove("doc")

If Not openWith.ContainsKey("doc") Then
    Console.WriteLine("Key ""doc"" is not found.")
End If

注解

SortedDictionary<TKey,TValue>如果不包含具有指定键的元素,则SortedDictionary<TKey,TValue>保持不变。 不会引发异常。

此方法是 O(log n) 操作。

适用于

另请参阅