OrderedDictionary.Insert(Int32,Object,Object)方法用于将新条目插入到OrderedDictionary集合中,并在指定索引处使用指定键和值。
句法:
public void Insert (int index, object key, object value);
参数:
index : It is the zero-based index at which the element should be inserted.
key : It is the key of the entry to add.
value : It is the value of the entry to add. The value can be null.
例外情况:
- NotSupportedException:如果OrderedDictionary集合为只读。
- ArgumentOutOfRangeException:如果索引超出范围。
下面给出了一些示例,以更好地理解实现:
范例1:
// C# code to insert entry into
// OrderedDictionary with key and
// value at the specified index
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
// Driver method
public static void Main()
{
// Creating a orderedDictionary named myDict
OrderedDictionary myDict = new OrderedDictionary();
// Adding key and value in myDict
myDict.Add("key1", "value1");
myDict.Add("key2", "value2");
myDict.Add("key3", "value3");
myDict.Add("key4", "value4");
myDict.Add("key5", "value5");
// Displaying the number of element initially
Console.WriteLine("Number of elements are : "
+ myDict.Count);
// Displaying the elements in myDict
foreach(DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " -- " + de.Value);
// Inserting entry into OrderedDictionary
// with key and value at the specified index
myDict.Insert(2, "Geeks", "Noida");
// Displaying the number of element initially
Console.WriteLine("Number of elements are : "
+ myDict.Count);
// Displaying the elements in myDict
foreach(DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " -- " + de.Value);
}
}
输出:
Number of elements are : 5
key1 -- value1
key2 -- value2
key3 -- value3
key4 -- value4
key5 -- value5
Number of elements are : 6
key1 -- value1
key2 -- value2
Geeks -- Noida
key3 -- value3
key4 -- value4
key5 -- value5
范例2:
// C# code to insert entry into
// OrderedDictionary with key and
// value at the specified index
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
// Driver method
public static void Main()
{
// Creating a orderedDictionary named myDict
OrderedDictionary myDict = new OrderedDictionary();
// Adding key and value in myDict
myDict.Add("A", "Apple");
myDict.Add("B", "Banana");
myDict.Add("C", "Cat");
myDict.Add("D", "Dog");
// Displaying the number of element initially
Console.WriteLine("Number of elements are : "
+ myDict.Count);
// Displaying the elements in myDict
foreach(DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " -- " + de.Value);
// Inserting entry into OrderedDictionary
// with key and value at the specified index
// This should raise "ArgumentOutOfRangeException"
// as index is out of range
myDict.Insert(10, "E", "Elephant");
// Displaying the number of element initially
Console.WriteLine("Number of elements are : "
+ myDict.Count);
// Displaying the elements in myDict
foreach(DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " -- " + de.Value);
}
}
运行时错误:
Unhandled Exception:
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index
笔记:
- 如果index参数等于OrderedDictionary集合中的条目数,则将key和value参数附加到集合的末尾。
- 插入点之后的条目将向下移动以容纳新条目,并且所移动条目的索引也会更新。
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.ordereddictionary.insert?view=netframework-4.7.2