c# - 默认构造函数创建的 `Dictionary`是否使用哈希码?

标签 c# .net dictionary

我需要使用我编写的类作为 Dictionary 的 key 类型

我读了documentation on MSDN about the default constructor of Dictionary

Dictionary<TKey, TValue> requires an equality implementation to determine whether keys are equal. This constructor uses the default generic equality comparer, EqualityComparer<T>.Default. If type TKey implements the System.IEquatable<T> generic interface, the default equality comparer uses that implementation. Alternatively, you can specify an implementation of the IEqualityComparer<T> generic interface by using a constructor that accepts a comparer parameter.

这让人觉得我唯一要做的就是让我的类(class)成为关键工具 System.IEquatable<T>

但是我很惊讶 System.IEquatable<T> 没有 HashCode()方法。

那么这样创建的字典会不会使用哈希码呢?如果是,它来自哪里?否则,我的词典会不会有恒定成本的访问操作(我认为没有哈希码是无法实现的)

最佳答案

However i'm very surprised that System.IEquatable doesn't have a HashCode() method.

这对于 System.IEquatable<T> 来说是多余的将 HashCode 方法设置为 System.Object (您的实现类将隐式继承)已经提供了方法 GetHashCode

关于c# - 默认构造函数创建的 `Dictionary`是否使用哈希码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13385656/

相关文章:

c# - 在 iTextSharp 中隐藏表格边框

c# - 为什么我不能在我自己的类中访问 UmbracoHelper

c# - Entity Framework SaveChanges 不起作用

c# - 在 ASP.Net 站点中使用 AJAX 调用时,REST WCF 服务会锁定线程

C# - 我可以根据用户提供的字符串公式动态定义函数吗?

c# - 如何在访问 Asp.net MVC 中的 View 文件夹时显示错误页面

python - 如何在python中使用变量作为字典的键

python - 如何遍历元组字典?

swift - 如何在 map View 中显示图钉上方的所有标题?

c# - 将数据表中的列相乘