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# Combine Dictionary Keys

Combine, or union, all the keys from several Dictionary instances. The keys must be of the same type.
Combine Dictionary keys. The keys of 2 Dictionaries can be combined. We union all the keys in several Dictionaries. This yields a collection of every key in the Dictionaries that we can display. We use the HashSet class for the best results.Dictionary
To start, we combine Dictionaries with the above keys using HashSet. The solution uses HashSet's UnionWith instance method. This combines the HashSet with another collection of the same value type.

Note: This is a complete console application that you can run in Visual Studio.

HashSet

Strings: We populate dictionaries with strings. The approach in this article would work on Dictionaries with any value type.

Part D: A new HashSet is created from the Keys. This HashSet is unioned on the other 2 Dictionary Keys properties.

C# program that uses HashSet using System; using System.Collections.Generic; class Program { static void Main() { // A. First dictionary. Dictionary<string, int> d1 = new Dictionary<string, int>(); d1.Add("cat", 1); d1.Add("dog", 2); // B. Second dictionary. Dictionary<string, int> d2 = new Dictionary<string, int>(); d2.Add("cat", 3); d2.Add("man", 4); // C. Third dictionary. Dictionary<string, int> d3 = new Dictionary<string, int>(); d3.Add("sheep", 5); d3.Add("fish", 6); d3.Add("cat", 7); // D. Get union of all keys. HashSet<string> h1 = new HashSet<string>(d1.Keys); h1.UnionWith(d2.Keys); h1.UnionWith(d3.Keys); // E. Display all the keys. // You can look up the values in the loop body. foreach (string k in h1) { Console.WriteLine(k); } } } Output cat dog man sheep fish
Discussion. Microsoft provides some examples of HashSet use. Here is the description of UnionWith: "Modifies the current HashSet(T) object to contain all elements that are present in both itself and in the specified collection."UnionWith Method: Microsoft Docs
This code is fairly fast, but the performance has not been extensively tested. What the code does is clear. If you didn't use HashSet, you would have to write more code. You could refactor the HashSet code into a method.
In my application, I had 3 Dictionaries, which were built from 3 different sources. I needed them to be separate. But I wanted to display them all together in an ASP.NET webpage displayed as HTML. The HashSet code was ideal.
Summary. We use HashSet with Dictionary keys to combine collections and find the union of the keys. HashSet has the ideal logic here. The HashSet type comes to our rescue with its excellent and fast UnionWith method.
© 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