Home »
.Net »
C# Programs
C# program to demonstrate the IDictionary interface
Here, we are going to demonstrate the IDictionary interface in C#?
Submitted by Nidhi, on November 05, 2020
Here, we will demonstrate the IDictionary interface with the help of Dictionary and SortedDictionary collection class.
Program:
The source code to demonstrate the IDictionary interface is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//C# program to demonstrate the IDictionary interface
using System;
using System.Collections.Generic;
class Demo
{
static void PrintDictionry(IDictionary<string, string> i)
{
Console.WriteLine(i["Name"]);
}
static void Main()
{
Dictionary<string, string> Student = new Dictionary<string, string>();
SortedDictionary<string, string> Employee = new SortedDictionary<string, string>();
Student["Name"] = "Virat";
PrintDictionry(Student);
Student["Name"] = "Rohit";
PrintDictionry(Student);
Employee["Name"] = "Saurabh";
PrintDictionry(Employee);
Employee["Name"] = "Sachin";
PrintDictionry(Employee);
}
}
Output:
Virat
Rohit
Saurabh
Sachin
Press any key to continue . . .
Explanation:
In the above program, we created a Demo class that contains two static methods Print() and Main(). In the Print() method, we receive an object that contains an IDictionary interface and then print the items based on the key on the console screen.
Now look to the Main() method, Here, we created the object of Dictionary and SortedDictionary class and then add items in the objects Student and Employee based on the Name key and then print them using the Print() method class on the console screen.
C# Basic Programs »