- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
This question Jon 的回答让我意识到这甚至存在,所以我很好奇并启动了 Visual Studio。
我遵循了 MSDN 页面的一个示例,然后创建了自己的小示例。如下:
public class Person : IEquatable<Person>
{
public string IdNumber { get; set; }
public string Name { get; set; }
public bool Equals(Person otherPerson)
{
if (IdNumber == otherPerson.IdNumber)
return true;
else
return false;
}
public override bool Equals(object obj)
{
if (obj == null)
return base.Equals(obj);
if (!(obj is Person))
throw new InvalidCastException("The Object isn't of Type Person.");
else
return Equals(obj as Person);
}
public override int GetHashCode()
{
return IdNumber.GetHashCode();
}
public static bool operator ==(Person person1, Person person2)
{
return person1.Equals(person2);
}
public static bool operator !=(Person person1, Person person2)
{
return (!person1.Equals(person2));
}
}
所以我有几个问题:
如果 Equals 方法在处理我的自定义相等方面做得很好,为什么我还必须重写 GetHashCode 方法?
当比较如下所示时,使用哪个比较器,Equals 还是 GetHashCode?
.
static void Main(string[] args)
{
Person sergio = new Person() { IdNumber = "1", Name = "Sergio" };
Person lucille = new Person() { IdNumber = "2", Name = "Lucille" };
List<Person> people = new List<Person>(){
sergio,
lucille
};
Person lucille2 = new Person() { IdNumber = "2", Name = "Lucille" };
if (people.Contains(lucille2))
{
Console.WriteLine("Already exists.");
}
Console.ReadKey();
}
最佳答案
If the Equals method does a good job at handling my custom equality, why do I have to override the GetHashCode method as well?
这允许您的类型用于通过散列工作的集合,例如作为 Dictionary<T, U>
中的键,或存储在 HashSet<T>
中.
When comparing something like below, which comparer is used, the Equals or the GetHashCode?
GetHashCode 不用于比较 - 仅用于散列操作。始终使用等于。
What exactly do the operator method do? It looks like some sort of voodoo black magic going on there.
这允许您直接使用 ==
在您的类型的两个实例上。否则,如果您的类型是一个类,您将通过引用进行比较,而不是通过您的类型中的值进行比较。
关于c# - 覆盖具有 'dibs' 的类型的 Equals 和 GetHashCode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5608267/
我有一个实现 IEquatable<> 的类 A,使用它的字段(比如 A.b 和 A.c)来实现/覆盖 Equals() 并覆盖 GetHashCode(),并且一切正常,99% 的时间。 A 类是继
为什么会这样17m.GetHashCode() == 17d.GetHashCode()(m =十进制,d =两倍) 此外,如预期的那样17f.GetHashCode() != 17d.GetHash
我找到了一个 GetHashCode() 的实现,看起来像这样 Guid _hashCode = Guid.NewGuid(); public override int GetHash
我想使用 Distinct()使用我的数据,声明为 IEnumerable> .在这种情况下,我必须实现自己的 IEqualityComparer还有我的问题: 以下实现之间有什么区别吗? publi
我认为这些集合的 GetHashCode 函数不将它们的哈希码基于列表中的项目是很奇怪的。 我需要它来工作以提供脏检查(您有未保存的数据)。我已经编写了一个覆盖 GetHashCode 方法的包装类,
明明有更好的hash方法 有位朋友对我吐槽前几天我列举的在源生成器的生成db映射实体的优化点 提前生成部分 hashcode 进行比较 所示代码 public static void Gener
我正在为在 .net 上运行的语言编写编译器,我希望它做的一件事是自动生成 GetHashCode 方法,但我有几个问题: 这是否可能,编译器是否足够了解所涉及的类型以合理地实现该方法? 我应该为值类
昨天我浏览了一些 .net 源代码,看到了一些 GetHashcode 的实现,其中包含以下内容: (i1 << 5) + i ^ i2 我了解代码在做什么以及为什么。我想知道的是他们为什么使用 (i
对于 Delphi 项目(使用 RAD Studio XE7 构建),我想创建一个画笔字典。每个字典项都包含一个 TMyBrush 对象作为键,该对象描述要检索的画笔,以及一个 GDI+ 画笔作为值。
我有以下类(class): public class Foo { int year; string name; int category; } 这是一些示
我有这个问题。 public class Foo : object { public override bool Equals(obj a, objb) { return
有时我需要没有字段(消息头、模式等)的值对象,例如: abstract class RequestHeader { } sealed class FirstRequestHeader : Reques
我正在使用 Linq-to-Sql 查询 SQL Server 数据库。此查询返回我的数据库中实体的列表。我的基础数据没有改变。 收到列表后,我会对其调用 GetHashCode 以测试是否相等。奇怪
我在这里阅读了一些与 GetHashCode 正确实现相关的问题。我没有找到的是什么时候我应该实现这个方法。 在我的具体案例中,我构建了一个简单的不可变结构: public struct MyStru
我正在使用 unity,而 unity 中没有元组,所以我创建了自己的元组类来工作,因为我的字典需要它。 Dictionary , Tile> 我创建的 Tile 类与解决这个问题并不相关(至少我认为
我在 Microsoft 文档中找到了以下内容: Two objects that are equal return hash codes that are equal. However, the r
我试着关注 Guidelines来自 MSDN,也引用了 This great question但下面的行为似乎并不像预期的那样。 我试图表示类似于 FQN 的结构,其中就好像 P1 在 P2 之前列
我最近用几种不同的方式问过这个问题,但没有得到告诉我如何使用 的字典的答案。当我持有对更改的内容的引用时需要处理 T.GetHashCode() .出于此问题的目的,“状态”指的是在 Equals(
我有以下类(class) public class ResourceInfo { public string Id { get; set; } public string Url {
首先,我使用 GetHashCode算法描述,here .现在,想象以下(人为的)示例: class Foo { public Foo(int intValue, double doubleV
我是一名优秀的程序员,十分优秀!