- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if two StringDictionary objects are equal or not in C#
To check if two StringDictionary objects are equal or not, the code is as follows −
Example
using System; using System.Collections.Specialized; public class Demo { public static void Main() { StringDictionary strDict1 = new StringDictionary(); strDict1.Add("A", "John"); strDict1.Add("B", "Andy"); strDict1.Add("C", "Tim"); strDict1.Add("D", "Ryan"); strDict1.Add("E", "Kevin"); strDict1.Add("F", "Katie"); strDict1.Add("G", "Brad"); StringDictionary strDict2 = new StringDictionary(); strDict2.Add("A", "John"); strDict2.Add("B", "Andy"); strDict2.Add("C", "Tim"); strDict2.Add("D", "Ryan"); strDict2.Add("E", "Kevin"); strDict2.Add("F", "Katie"); strDict2.Add("G", "Brad"); StringDictionary strDict3 = new StringDictionary(); strDict3 = strDict2; Console.WriteLine("Is Dictionary2 equal to Dictionary3? = "+strDict2.Equals(strDict3)); Console.WriteLine("Is Dictionary1 equal to Dictionary3? = "+strDict1.Equals(strDict3)); } }
Output
This will produce the following output −
Is Dictionary2 equal to Dictionary3? = True Is Dictionary1 equal to Dictionary3? = False
Example
Let us see another example −
using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main() { StringDictionary strDict1 = new StringDictionary(); strDict1.Add("A", "John"); strDict1.Add("B", "Andy"); strDict1.Add("C", "Tim"); strDict1.Add("D", "Ryan"); strDict1.Add("E", "Kevin"); strDict1.Add("F", "Katie"); strDict1.Add("G", "Brad"); Console.WriteLine("StringDictionary1 elements..."); foreach(DictionaryEntry d in strDict1) { Console.WriteLine(d.Key + " " + d.Value); } StringDictionary strDict2 = new StringDictionary(); strDict2.Add("A", "John"); strDict2.Add("B", "Andy"); strDict2.Add("C", "Tim"); strDict2.Add("D", "Ryan"); strDict2.Add("E", "Kevin"); strDict2.Add("F", "Katie"); strDict2.Add("G", "Brad"); Console.WriteLine("
StringDictionary2 elements..."); foreach(DictionaryEntry d in strDict2) { Console.WriteLine(d.Key + " " + d.Value); } Console.WriteLine("
Is Dictionary2 equal to Dictionary1? = "+strDict2.Equals(strDict1)); } }
Output
This will produce the following output −
StringDictionary1 elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad StringDictionary2 elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad Is Dictionary2 equal to Dictionary1? = False
Advertisements