OrderedDictionary.Remove(Object) 方法
定义
从 OrderedDictionary 集合中移除具有指定键的项。Removes the entry with the specified key from the OrderedDictionary collection.
public:
virtual void Remove(System::Object ^ key);
public void Remove (object key);
abstract member Remove : obj -> unit
override this.Remove : obj -> unit
Public Sub Remove (key As Object)
参数
- key
- Object
要移除的条目的键。The key of the entry to remove.
实现
例外
OrderedDictionary 集合为只读。The OrderedDictionary collection is read-only.
key 为 null。key is null.
示例
下面的代码示例演示了对集合的修改 OrderedDictionary 。The following code example demonstrates the modification of an OrderedDictionary collection. 在此示例中, Remove 使用方法从中删除具有键的项 "keyToDelete" OrderedDictionary 。In this example, the Remove method is used to remove the entry with the key "keyToDelete" from the OrderedDictionary. 此代码是可以在中查看的更大代码示例的一部分 OrderedDictionary 。This code is part of a larger code example that can be viewed at OrderedDictionary.
// Modifying the OrderedDictionary
if (!myOrderedDictionary->IsReadOnly)
{
// Insert a new key to the beginning of the OrderedDictionary
myOrderedDictionary->Insert(0, "insertedKey1", "insertedValue1");
// Modify the value of the entry with the key "testKey2"
myOrderedDictionary["testKey2"] = "modifiedValue";
// Remove the last entry from the OrderedDictionary: "testKey3"
myOrderedDictionary->RemoveAt(myOrderedDictionary->Count - 1);
// Remove the "keyToDelete" entry, if it exists
if (myOrderedDictionary->Contains("keyToDelete"))
{
myOrderedDictionary->Remove("keyToDelete");
}
}
// Modifying the OrderedDictionary
if (!myOrderedDictionary.IsReadOnly)
{
// Insert a new key to the beginning of the OrderedDictionary
myOrderedDictionary.Insert(0, "insertedKey1", "insertedValue1");
// Modify the value of the entry with the key "testKey2"
myOrderedDictionary["testKey2"] = "modifiedValue";
// Remove the last entry from the OrderedDictionary: "testKey3"
myOrderedDictionary.RemoveAt(myOrderedDictionary.Count - 1);
// Remove the "keyToDelete" entry, if it exists
if (myOrderedDictionary.Contains("keyToDelete"))
{
myOrderedDictionary.Remove("keyToDelete");
}
}
' Modifying the OrderedDictionary
If Not myOrderedDictionary.IsReadOnly Then
' Insert a new key to the beginning of the OrderedDictionary
myOrderedDictionary.Insert(0, "insertedKey1", "insertedValue1")
' Modify the value of the entry with the key "testKey2"
myOrderedDictionary("testKey2") = "modifiedValue"
' Remove the last entry from the OrderedDictionary: "testKey3"
myOrderedDictionary.RemoveAt(myOrderedDictionary.Count - 1)
' Remove the "keyToDelete" entry, if it exists
If (myOrderedDictionary.Contains("keyToDelete")) Then
myOrderedDictionary.Remove("keyToDelete")
End If
End If
注解
删除项之后的项将上移以占据空出的点,还会更新移动项的索引。The entries that follow the removed entry move up to occupy the vacated spot and the indexes of the entries that move are also updated.
如果 OrderedDictionary 集合不包含具有指定键的项,则将 OrderedDictionary 保持不变,并且不会引发异常。If the OrderedDictionary collection does not contain an entry with the specified key, the OrderedDictionary remains unchanged and no exception is thrown.