gpt4 book ai didi

c# - Dictionary 会使用 TKey.Equals 和 TKey.GetHashCode 吗?

转载 作者:太空狗 更新时间:2023-10-30 00:33:24 29 4
gpt4 key购买 nike

我实现了一个字典如下:

Dictionary<ErrorHashKey, ErrorRow> dictionary;

我已经定义了Equals()GetHashCode()ErrorHashKey类(class)。我目前正在为该项目编写一些文档,并且是从 IEqualityComparer Interface doc 中看到的。 :

Dictionary requires an equality implementation to determine whether keys are equal. You can specify an implementation of the IEqualityComparer generic interface by using a constructor that accepts a comparer parameter; if you do not specify an implementation, the default generic equality comparer EqualityComparer.Default is used. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation.

我没有做文档指定的任何事情(或者至少我认为我没有做)。我没有在构造函数参数中传递比较器,也没有创建 EqualityComparer.Default比较器。

System.IEquatable<T> generic interface在创建的每个类中自动实现?我应该定义 IEqualityComparer<T> 的实现吗? ?

最佳答案

默认比较器会调用object.Equalsobject.GetHashCode (你重写的方法)如果IEquatable<T>未实现。这记录在 documentation for EqualityComparer<T>.Default .你不需要做任何额外的事情,不,IEquatable<T>不会在您的类(class)中自动实现。

关于c# - Dictionary<TKey, TValue> 会使用 TKey.Equals 和 TKey.GetHashCode 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12057315/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com