自定义类是C#中字典的关键

发布于 2025-02-08 23:53:27 字数 1690 浏览 2 评论 0原文

我最近开始研究C#,但在这种特殊情况下陷入困境。是的,我已经解决了以前的问题,但无法理解我的代码中的问题。 自定义类用作词典中的键,但不是键找到 这是我能找到的最接近的答案,但是这里给出的答案似乎对我不起作用。

我有一个字典注册表< string>。 客户是一个具有2个字符串的课程:S1和S2。客户可以匹配S1或S2(不需要两者)。

班级客户:

public class Customers
    {
        public string _s1;
        public string _s2;

        public Customers(string s1, string s2)
        {
            _s1 = s1;
            _s2 = s2;
        }

        public class EqualityComparer : IEqualityComparer<Customers>
        {
            public bool Equals(Customers x, Customers y)
            {
                Console.WriteLine("Inside Equals");
                return ((x._s1.Equals(y._s1)) || (x._s2.Equals(y._s2)));
            }
            public int GetHashCode(Customers x)
            {
                return x._s1.GetHashCode() ^ x._s2.GetHashCode();
            }
        }
    }

program.cs:

Dictionary<Customers, string> registry = new Dictionary<Customers, string>(new Customers.EqualityComparer());

Customers key = new Customers("1", "2");
Customers key2 = new Customers("12", "21");
registry.Add(key, "12");
registry.Add(key2, "22");
Customers lookUp1 = new Customers("1", "2");
Customers lookUp2 = new Customers("1", "32");
if (registry.ContainsKey(lookUp2))
{
    Console.WriteLine("Found");
}
else
{
    Console.WriteLine("Not Found");
}

问题是,当S1和S2都匹配时,我就会找到“找到”,但是当其中只有一个我得到“找不到”时,尽管已更改了Equals()并给出参考构造函数中新的EqualityComparer。

另外,对于Lookup1,我确实得到了“内部等式”,但对于Lookup2而言,我不确定为什么。

I recently started studying C# but am stuck on this particular case. Yes I have gone over the previous questions but was not able to get what the issue in my code is.
Custom Class used as key in Dictionary but key not found
This is the closest one I could find, but the answer given here does not seem to work for me.

I have a Dictionary registry<Customers, string>.
Customers is a class that has 2 strings: s1 and s2. A customer can match either s1 or s2 (both are not required).

Class Customers:

public class Customers
    {
        public string _s1;
        public string _s2;

        public Customers(string s1, string s2)
        {
            _s1 = s1;
            _s2 = s2;
        }

        public class EqualityComparer : IEqualityComparer<Customers>
        {
            public bool Equals(Customers x, Customers y)
            {
                Console.WriteLine("Inside Equals");
                return ((x._s1.Equals(y._s1)) || (x._s2.Equals(y._s2)));
            }
            public int GetHashCode(Customers x)
            {
                return x._s1.GetHashCode() ^ x._s2.GetHashCode();
            }
        }
    }

Program.cs:

Dictionary<Customers, string> registry = new Dictionary<Customers, string>(new Customers.EqualityComparer());

Customers key = new Customers("1", "2");
Customers key2 = new Customers("12", "21");
registry.Add(key, "12");
registry.Add(key2, "22");
Customers lookUp1 = new Customers("1", "2");
Customers lookUp2 = new Customers("1", "32");
if (registry.ContainsKey(lookUp2))
{
    Console.WriteLine("Found");
}
else
{
    Console.WriteLine("Not Found");
}

The issue is that when both s1 and s2 are matching, then I get "Found", but when only one of them does I get "Not found" despite having changed Equals() accordingly and giving the reference of the new EqualityComparer in the Constructor as well.

Also, For lookup1 I do get "Inside Equals" but not for lookup2 and I'm not sure why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您有 equality 设计的问题。每个平等通信必须遵循以下规则:

  1. A.Equals(A)== True对于所有a
  2. 如果A.Equals(b)== true,则B. ALL a,b的equal(a)== true
  3. 如果A.Equals(b)&amp;&amp; B. equals(c)== true然后A. equal(c)== true to all a,b,c

如我们所见,规则#3(传递规则)是损坏的

   Customer A = new Customer("x", "y");
   Customer B = new Customer("x", "z");
   Customer C = new Customer("p", "z");

由于平等是错误地实现的 't工作正确

You have a problem with equality design. Each equality correspondence must follow these rules:

  1. A.Equals(A) == true for all A.
  2. if A.Equals(B) == true then B.Equals(A) == true for all A, B.
  3. if A.Equals(B) && B.Equals(C) == true then A.Equals(C) == true for all A, B, C.

As we can see, the rule #3 (transitive rule) is broken:

   Customer A = new Customer("x", "y");
   Customer B = new Customer("x", "z");
   Customer C = new Customer("p", "z");

Since equality is implemented incorrectly, Dictionary<K, V> doesn't work properly.

绮筵 2025-02-15 23:53:27

发生这种情况是因为字典基本上是 hash table

通过使用其密钥检索值非常快,接近o(1),因为dictionary&lt; tkey,tvalue&gt; class class被实现为 hash table

匹配的第一个规则是哈希码应相等,“ 1” .gethashcode() ^“ 2” .gethashcode()代码>“ 1” .gethashcode() ^“ 32” .gethashcode()(用于lookup2)。

This happens because Dictionary is basically a hash table:

Retrieving a value by using its key is very fast, close to O(1), because the Dictionary<TKey,TValue> class is implemented as a hash table.

and first rule for matching is that hashcodes should be equal and "1".GetHashCode() ^ "2".GetHashCode() (for key) will definitely differ from "1".GetHashCode() ^ "32".GetHashCode()(for lookUp2).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文