📌  相关文章
📜  C#|使用指定的键和值在OrderedDictionary中插入一个新条目

📅  最后修改于: 2021-05-30 01:20:44             🧑  作者: Mango

OrderedDictionary.Insert(Int32,Object,Object)方法用于将新条目插入到OrderedDictionary集合中,并在指定索引处使用指定键和值。

句法:

public void Insert (int index, object key, object value);

参数:

例外情况:

  • ArgumentOutOfRangeException:如果索引超出范围。
  • NotSupportedException:如果集合是只读的。

下面的程序说明了上面讨论的方法的使用:

范例1:

// C# code to add a new entry into the
// OrderedDictionary collection with
// the specified 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");
  
        Console.WriteLine("Before Updation: ");
  
        // Displaying the key/value pairs in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " --> " + de.Value);
  
        // using Insert(Int32, Object, Object) Method
        // to add a new entry at index 5
        myDict.Insert(5, "key6", "value6");
  
        Console.WriteLine("\nAfter Updation: ");
  
        // Displaying the key/value pairs in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " --> " + de.Value);
    }
}

输出:

Before Updation: 
key1 --> value1
key2 --> value2
key3 --> value3
key4 --> value4
key5 --> value5

After Updation: 
key1 --> value1
key2 --> value2
key3 --> value3
key4 --> value4
key5 --> value5
key6 --> value6

范例2:

// C# code to add a new entry into the
// OrderedDictionary collection with
// the specified 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("1", "C");
        myDict.Add("2", "C++");
        myDict.Add("3", "Java");
        myDict.Add("4", "Python");
        myDict.Add("5", "C#");
  
        Console.WriteLine("Before Updation: ");
  
        // Displaying the key/value pairs in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " --> " + de.Value);
  
        // using Insert(Int32, Object, Object) Method
        // this will give error as index is out of range
        // here indexing is zero-based
        myDict.Insert(6, "6", "HTML");
  
        Console.WriteLine("\nAfter Updation: ");
  
        // Displaying the key/value pairs in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " --> " + de.Value);
    }
}

运行时错误:

笔记:

  • 如果index参数等于OrderedDictionary集合中的条目数,则key和value参数将附加到集合的末尾。
  • 插入点之后的条目将向下移动以容纳新条目,并且所移动条目的索引也会更新。

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.ordereddictionary.insert?view=netframework-4.7.2