Create OrderedDictionary with initial capacity in CSharp

Description

The following code shows how to create OrderedDictionary with initial capacity.

Example


using System;//w w w .  j  a va  2  s.c  om
using System.Collections;
using System.Collections.Specialized;

public class SamplesStringCollection  {

   public static void Main()  {
        OrderedDictionary myOrderedDictionary = new OrderedDictionary(10);
        myOrderedDictionary.Add("testKey1", "testValue1");
        myOrderedDictionary.Add("testKey2", "testValue2");
        myOrderedDictionary.Add("testKey3", "testValue3");

        ICollection keyCollection = myOrderedDictionary.Keys;
        ICollection valueCollection = myOrderedDictionary.Values;
        
        DisplayContents(keyCollection, valueCollection, myOrderedDictionary.Count);

   }
    public static void DisplayContents(ICollection keyCollection, ICollection valueCollection, int dictionarySize)
    {
        String[] myKeys = new String[dictionarySize];
        String[] myValues = new String[dictionarySize];
        keyCollection.CopyTo(myKeys, 0);
        valueCollection.CopyTo(myValues, 0);
    
        for (int i = 0; i < dictionarySize; i++)
        {
            Console.WriteLine(myKeys[i] + "" + myValues[i]);
        }
    
    }

}

The code above generates the following result.





















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary