自定义类是C#中字典的关键
我最近开始研究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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有 equality 设计的问题。每个平等通信必须遵循以下规则:
A.Equals(A)== True
对于所有a
。A.Equals(b)== true
,则B. ALL
。a,b
的equal(a)== trueA.Equals(b)&amp;&amp; B. equals(c)== true
然后A. equal(c)== true
to alla,b,c
。如我们所见,规则#3(传递规则)是损坏的:
由于平等是错误地实现的 't工作正确。
You have a problem with equality design. Each equality correspondence must follow these rules:
A.Equals(A) == true
for allA
.A.Equals(B) == true
thenB.Equals(A) == true
for allA, B
.A.Equals(B) && B.Equals(C) == true
thenA.Equals(C) == true
for allA, B, C
.As we can see, the rule #3 (transitive rule) is broken:
Since equality is implemented incorrectly,
Dictionary<K, V>
doesn't work properly.发生这种情况是因为
字典
基本上是 hash table :匹配的第一个规则是哈希码应相等,
“ 1” .gethashcode() ^“ 2” .gethashcode()代码>“ 1” .gethashcode() ^“ 32” .gethashcode()
(用于lookup2
)。This happens because
Dictionary
is basically a hash table:and first rule for matching is that hashcodes should be equal and
"1".GetHashCode() ^ "2".GetHashCode()
(forkey
) will definitely differ from"1".GetHashCode() ^ "32".GetHashCode()
(forlookUp2
).