How to create an OrderedDictionary in C#
Last Updated :
27 Feb, 2019
OrderedDictionary() constructor is used to initialize a new instance of the OrderedDictionary class which will be empty and will have the default initial capacity. OrderedDictionary Class represents a collection of key/value pairs that are accessible by the key or index. It is present in System.Collections.Specialized
namespace.
Syntax:
public OrderedDictionary();
Example 1:
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main(String[] args)
{
OrderedDictionary od = new OrderedDictionary();
Console.WriteLine( "The number of elements: "
+od.Count);
}
}
|
Output:
The number of elements: 0
Example 2:
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main(String[] args)
{
OrderedDictionary od = new OrderedDictionary();
Console.Write( "Before Add Method: " );
Console.WriteLine(od.Count);
od.Add( "1" , "C" );
od.Add( "2" , "C++" );
od.Add( "3" , "Java" );
od.Add( "4" , "C#" );
Console.Write( "After Add Method: " );
Console.WriteLine(od.Count);
}
}
|
Output:
Before Add Method: 0
After Add Method: 4
Reference:
Share your thoughts in the comments
Please Login to comment...