TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# IDictionary Generic Interface

Use the IDictionary generic interface. IDictionary is implemented by the Dictionary and SortedDictionary.
IDictionary. How can we use the IDictionary interface? Many lookup types (Dictionary, SortedDictionary) implement the IDictionary interface. We can make types that implement IDictionary. But it is useful too as an abstraction.DictionaryInterface
Example. 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.

Tip: This code only needs to be written once if you have it use the IDictionary type.

Next: The WriteKeyA method works equally well on Dictionary and SortedDictionary instances.

C# program that uses IDictionary type using System; using System.Collections.Generic; class Program { static void Main() { // Dictionary implements IDictionary. Dictionary<string, string> dict = new Dictionary<string, string>(); dict["A"] = "B"; WriteKeyA(dict); // SortedDictionary implements IDictionary. SortedDictionary<string, string> sort = new SortedDictionary<string, string>(); sort["A"] = "C"; WriteKeyA(sort); } static void WriteKeyA(IDictionary<string, string> i) { // Use instance through IDictionary interface. Console.WriteLine(i["A"]); } } Output B C
Fields and variables. It is also possible to have fields of type IDictionary. This can make it possible to have a class that can use any dictionary type without worrying about which one it is.Class

Tip: You could even later implement a custom Dictionary type and never need to change this class. Variables can use type IDictionary.

Discussion. The IDictionary type has many required methods. I don't present an implementation of IDictionary here because the Dictionary type itself is good at what it does. An alternative implementation would not be of much use in most programs.

Further: Even the alternatives in the .NET Framework, such as SortedDictionary, are typically not useful.

SortedDictionary
Summary. We looked at the IDictionary type. 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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf