C# Program to Demonstrate the iDictionary Interface
Posted by Superadmin on August 12 2022 05:49:36

C# Program to Demonstrate the iDictionary Interface

 

This is a C# Program to demonstrate idictionary interface.

Problem Description

This C# Program Demonstrates iDictionary Interface.

Problem Solution

Here this program uses both the Dictionary and SortedDictionary types. Suppose that you want to add some functionality that can work on an instance of Dictionary or an instance of SortedDictionary.

Program/Source Code

Here is source code of the C# Program to Demonstrate iDictionary Interface. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Demonstrate iDictionary Interface
 */
using System;
using System.Collections.Generic;
 
class Program
{
    static void Main()
    {
       Dictionary<string, string> dict = new Dictionary<string, string>();
       dict["Tom"] = "Bob";
       WriteKeyA(dict);
       SortedDictionary<string, string> sort = new SortedDictionary<string, string>();
       sort["Tom"] = "Jerry";
       WriteKeyA(sort);
       Console.ReadLine();
    }
 
    static void WriteKeyA(IDictionary<string, string> i)
    {
 
       Console.WriteLine(i["Tom"]);
    }
}
Program Explanation

This C# program is used to demonstrate the IDictionary interface. This program uses both the Dictionary and SortedDictionary types. To add some functionality that can work on an instance of Dictionary or an instance of SortedDictionary.

 

It can be used in an elaborate implementation of a custom dictionary. It can also be used in simpler programs that act upon different dictionary types including Dictionary and SortedDictionary.
The Dictionary implements the IDictionary interface. SortedDictionary implements IDictionary. In Console.WriteLine(i[“Tom”]) by using instance through IDictionary interface.

Runtime Test Cases
 
Bob
Jerry