gpt4 book ai didi

c# - 为什么对于 C# 中 byte[] 类型的键,哈希表不会为 "ContainsKey"返回 true?

转载 作者:行者123 更新时间:2023-11-30 19:13:57 25 4
gpt4 key购买 nike

考虑以下代码:

byte[] bytes = new byte[] { 1, 2, 5, 0, 6 };
byte[] another = new byte[] { 1, 2, 5, 0, 6 };

Hashtable ht = new Hashtable();
ht.Add(bytes, "hi");
Assert.IsTrue(ht.ContainsKey(another));

为什么这个断言会失败?作为原始类型的数组不应该使用对象引用,不是吗?那么为什么它会返回 false 呢?我可以做些什么来使这个哈希表正常工作吗?

最佳答案

Being an array of a primitive type shouldn't use using the object reference, should it?

是的,它应该。数组是引用类型。

一切正常。

如果您想要不同的行为,您可以为数组实现一个比较器来比较内容并将其传递给哈希表。

关于c# - 为什么对于 C# 中 byte[] 类型的键,哈希表不会为 "ContainsKey"返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1905038/

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