相关疑难解决方法(0)

在字典中使用byte []作为键

我需要byte[]在a中使用a 作为键Dictionary.由于byte[]不会覆盖默认GetHashCode方法,byte[]因此包含相同数据的两个单独对象将在字典中使用两个单独的插槽.基本上我想要的是这个:

Dictionary<byte[], string> dict = new Dictionary<byte[], string>();
dict[new byte[] {1,2,3}] = "my string";
string str = dict[new byte[] {1,2,3}];
// I'd like str to be set to "my string" at this point
Run Code Online (Sandbox Code Playgroud)

有一个简单的方法吗?我唯一能想到的就是构建一个包装类,它只包含一个基于内容的byte[]覆盖,但这似乎容易出错.GetHashCodebyte[]

c#

39
推荐指数
2
解决办法
2万
查看次数

标签 统计

c# ×1