具有整数数组作为键的字典 [英] Dictionary with integer array as a key

查看:185
本文介绍了具有整数数组作为键的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个字典,其关键字是一个整数数组,例如 Dictionary< int [],string>

I need a Dictionary whose key is an array of integers for example Dictionary<int[],string> or

Dictionary<List<int>,string>.

但我很惊讶的是,Equality方法和哈希码方法没有为我定义。除了创建自己的 MyType:List< int> 并定义所有必要的方法,有没有任何简单的方法来实现这样的结构?

But I am quite surprised that the Equality method and hash code method is not defined for me. Is there any easy way to implement such a structure other than creating my own MyType: List<int> and to define all necessary methods?

推荐答案

它未预定义,因为它是昂贵的。如果你知道你的列表很短,那么只是实现明显的覆盖。如果不是,你必须为至少GetHashCode提出一些启发式。说,只有第一对元素xor-ed连同长度的GetHashCode。

It isn't predefined because it is expensive. If you know your list is short then just implement the obvious overrides. If not, you'll have to come up with some kind of heuristic for at least GetHashCode. Say, GetHashCode of only the first couple of elements xor-ed together with the Length.

这篇关于具有整数数组作为键的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆