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

定义

SortedList<TKey,TValue> 中移除包含指定键的元素。Removes the element with the specified key from the SortedList<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

要移除的元素的键。The key of the element to remove.

返回

如果该元素已成功移除,则为 true;否则为 falsetrue if the element is successfully removed; otherwise, false. 如果在原始 SortedList<TKey,TValue> 中没有找到 key,此方法也会返回 falseThis method also returns false if key was not found in the original SortedList<TKey,TValue>.

实现

异常

keynullkey is null.

示例

下面的代码示例演示如何使用Remove方法从排序列表中移除键/值对。The following code example shows how to remove a key/value pair from the sorted list using the Remove method.

此代码示例是为SortedList<TKey,TValue>类提供的更大示例的一部分。This code example is part of a larger example provided for the SortedList<TKey,TValue> class.

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

注解

此方法执行二进制搜索;但是,元素将上移以填充打开的位置,因此此方法是 O (n)运算,其中nCountThis method performs a binary search; however, the elements are moved up to fill in the open spot, so this method is an O(n) operation, where n is Count.

适用于

另请参阅