- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
当在 byte[]
数组上调用时,GetHashCode()
计算什么?具有相同内容的 2 个数据数组不提供相同的散列。
最佳答案
.NET 中的数组不会覆盖 Equals
或 GetHashCode
,因此您将获得的值基本上基于引用相等性(即 Object
中的默认实现) - 对于值相等性,您需要推出自己的代码(或从第三方找到一些代码)。您可能想要实现 IEqualityComparer<byte[]>
如果您尝试使用字节数组作为字典中的键等。
编辑:这是一个可重用的数组相等比较器,只要数组元素适本地处理相等性,它就应该没问题。请注意,在将数组用作字典中的键后,您不得改变数组,否则您将无法再次找到它 - 即使使用相同的引用也是如此。
using System;
using System.Collections.Generic;
public sealed class ArrayEqualityComparer<T> : IEqualityComparer<T[]>
{
// You could make this a per-instance field with a constructor parameter
private static readonly EqualityComparer<T> elementComparer
= EqualityComparer<T>.Default;
public bool Equals(T[] first, T[] second)
{
if (first == second)
{
return true;
}
if (first == null || second == null)
{
return false;
}
if (first.Length != second.Length)
{
return false;
}
for (int i = 0; i < first.Length; i++)
{
if (!elementComparer.Equals(first[i], second[i]))
{
return false;
}
}
return true;
}
public int GetHashCode(T[] array)
{
unchecked
{
if (array == null)
{
return 0;
}
int hash = 17;
foreach (T element in array)
{
hash = hash * 31 + elementComparer.GetHashCode(element);
}
return hash;
}
}
}
class Test
{
static void Main()
{
byte[] x = { 1, 2, 3 };
byte[] y = { 1, 2, 3 };
byte[] z = { 4, 5, 6 };
var comparer = new ArrayEqualityComparer<byte>();
Console.WriteLine(comparer.GetHashCode(x));
Console.WriteLine(comparer.GetHashCode(y));
Console.WriteLine(comparer.GetHashCode(z));
Console.WriteLine(comparer.Equals(x, y));
Console.WriteLine(comparer.Equals(x, z));
}
}
关于c# - byte[] 数组上的 GetHashCode(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7244699/
我有一个实现 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
我是一名优秀的程序员,十分优秀!