C# - IDictionary Interface Example

In this example, we will learn about the implementation of IDictionary interface using C# program?
Submitted by Nidhi, on November 05, 2020 [Last updated : March 22, 2023]

Here, we will demonstrate the IDictionary interface with the help of Dictionary and SortedDictionary collection classes.

C# program to demonstrate the example of IDictionary interface

The source code to demonstrate the IDictionary interface is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# - IDictionary Interface Example

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 »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.