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# Dictionary Equals: If Contents Are the Same

Test two Dictionary instances for equality of all elements, where ordering does not matter.
Dictionary, Equals. Two Dictionaries are equal if their contents are the same. A Dictionary is a reference type—its bits cannot just be checked. Further, 2 Dictionaries may have different orders of keys, even if they are equal.
As we begin, please note that it would be possible to create a generic method abstraction of this logic. This article does not use that syntax. Two Dictionary instances are allocated upon the managed heap at program runtime.DictionaryGeneric Class, Method

And: In this example, each Dictionary has three key-value pairs. The data is equal in every way.

Then: Counts are tested. Every key from the first Dictionary is looked up in the second, and its value is also checked.

C# program that tests Dictionaries for equality using System; using System.Collections.Generic; class Program { static void Main() { // Create a dictionary and add several elements to it. var dict = new Dictionary<string, int>(); dict.Add("cat", 2); dict.Add("dog", 3); dict.Add("x", 4); // Create another dictionary. var dict2 = new Dictionary<string, int>(); dict2.Add("cat", 2); dict2.Add("dog", 3); dict2.Add("x", 4); // Test for equality. bool equal = false; if (dict.Count == dict2.Count) // Require equal count. { equal = true; foreach (var pair in dict) { int value; if (dict2.TryGetValue(pair.Key, out value)) { // Require value be equal. if (value != pair.Value) { equal = false; break; } } else { // Require key be present. equal = false; break; } } } Console.WriteLine(equal); } } Output True
Notes, algorithm. The algorithm used has several details. If you do not correctly add every constraint, it will not return correct results. The counts of the two dictionaries must be equal.
This prevents a larger second dictionary from matching a smaller first. Every key must be found, and its value must be equal in both dictionaries. If the key is not found, the match fails.
Summary. It is possible to compare two Dictionary instances for equality using a custom method with key-value pair checking. There are other possible implementations, but this one is fairly resource-efficient and also simple.

Tip: When testing this method, make sure to change keys and values in both dictionaries.

And: As always, incorrect code is worse than no code at all, particularly if you are doing something important.

© 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