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.

advertisement
advertisement

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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.