C# में दो ListDictionary ऑब्जेक्ट्स समान हैं या नहीं यह जांचने के लिए, हम Equals() मेथड का उपयोग करते हैं। डिफ़ॉल्ट रूप से Equals() रेफरेंस समानता की जांच करता है (यानी दोनों वेरिएबल्स एक ही ऑब्जेक्ट को संदर्भित करते हैं)। ListDictionary सामग्री-आधारित समानता को ओवरराइड नहीं करता, इसलिए दो अलग-अलग इंस्टेंस जिनमें समान डेटा हो, false लौटाएंगे।
उदाहरण 1: रेफरेंस असाइनमेंट के साथ समानता जांच
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
ListDictionary dict1 = new ListDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("ListDictionary1 तत्व...");
foreach (DictionaryEntry d in dict1)
{
Console.WriteLine(d.Key + " " + d.Value);
}
ListDictionary dict2 = new ListDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
Console.WriteLine("\nListDictionary2 तत्व...");
foreach (DictionaryEntry d in dict2)
{
Console.WriteLine(d.Key + " " + d.Value);
}
ListDictionary dict3 = new ListDictionary();
dict3 = dict2; // रेफरेंस असाइनमेंट
Console.WriteLine("\nक्या ListDictionary3, ListDictionary2 के बराबर है? = " + (dict3.Equals(dict2)));
}
}
आउटपुट
ListDictionary1 तत्व... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear ListDictionary2 तत्व... 1 One 2 Two 3 Three 4 Four 5 Five 6 Six क्या ListDictionary3, ListDictionary2 के बराबर है? = True
उदाहरण 2: समान सामग्री वाले अलग-अलग इंस्टेंस की तुलना
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
ListDictionary dict1 = new ListDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("ListDictionary1 तत्व...");
foreach (DictionaryEntry d in dict1)
{
Console.WriteLine(d.Key + " " + d.Value);
}
ListDictionary dict2 = new ListDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
Console.WriteLine("\nListDictionary2 तत्व...");
foreach (DictionaryEntry d in dict2)
{
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nक्या ListDictionary1, ListDictionary2 के बराबर है? = " + (dict1.Equals(dict2)));
}
}
आउटपुट
ListDictionary1 तत्व... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear ListDictionary2 तत्व... 1 One 2 Two 3 Three 4 Four 5 Five 6 Six क्या ListDictionary1, ListDictionary2 के बराबर है? = False
मुख्य बिंदु:
Equals()डिफ़ॉल्ट रूप से रेफरेंस समानता की जांच करता हैdict3 = dict2दोनों को एक ही ऑब्जेक्ट की ओर इंगित करता है - इसलिएtrue- अलग-अलग इंस्टेंस जिनमें समान डेटा हो,
falseलौटाएंगे - सामग्री-आधारित तुलना के लिए मैन्युअल रूप से कुंजी-मान जोड़ों की तुलना करें
- या LINQ का उपयोग करें:
dict1.OrderBy(x => x.Key).SequenceEqual(dict2.OrderBy(x => x.Key)) ListDictionaryछोटे संग्रह (≤10 आइटम) के लिए अनुकूलित है