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

问题描述

我需要使用字节[] 如在词典一键。由于字节[] 不覆盖默认的GetHashCode 方法,两个独立的字节[] 包含相同的数据将在字典中使用两个单独的插槽对象。基本上我想是这样的:

 词典<字节[],字符串>字典=新词典<字节[],字符串>(); 
字典[新字节[] {1,2,3}] =我的字符串;
字符串str =字典[新字节[] {1,2,3}]。
//我想STR设置为我的字符串在这一点上

有一个简单的方法来做到这一点?我能想到的唯一的事情是建立一个只包含一个包装类字节[] 和覆盖的GetHashCode 基于对字节[] ,但这似乎容易出错。


解决方案

默认情况下的byte []将参照相比,是不是你在这种情况下想要的东西。你需要做的是指定自定义的的IEqualityComparer<字节[]> 和你想的比较。例如:



 公共类ByteArrayComparer:&的IEqualityComparer LT;字节[]> {
公共布尔等于(字节[]离开,字节[]右){
如果(左== NULL ||右== NULL){
返回左==权利;
}
返回left.SequenceEqual(右);
}
公众诠释的GetHashCode(字节[]键){
如果(键== NULL)
抛出新的ArgumentNullException(钥匙);
返回key.Sum(B = GT; B);
}
}



然后,你可以做

  VAR字典=新词典<字节[],字符串>(新ByteArrayComparer()); 



解决方案2.0



 公共类ByteArrayComparer:&的IEqualityComparer LT;字节[]> {
公共布尔等于(字节[]离开,字节[]右){
如果(左== NULL ||右== NULL){
返回左==权利;
}
如果(left.Length = right.Length!){
返回false;
}
的for(int i = 0; I< left.Length;我++){$​​ B $ B如果(左[I] =正确的[I]!){
返回假;
}
}
返回真;
}
公众诠释的GetHashCode(字节[]键){
如果(键== NULL)
抛出新的ArgumentNullException(钥匙);
INT总和= 0;
的foreach(重点字节CUR){
总和+ = CUR;
}
返回总和;
}
}


I need to use a byte[] as a key in a Dictionary. Since byte[] doesn't override the default GetHashCode method, two separate byte[] objects that contain the same data will use two separate slots in the dictionary. Basically what I want is this:

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

Is there a simple way to do this? The only thing I can think of would be to build a wrapper class that just contains a byte[] and override GetHashCode based on the contents of the byte[], but this seems error prone.

解决方案

By default byte[] will be compared by reference which is not what you want in this case. What you need to do is specify a custom IEqualityComparer<byte[]> and do the comparison you want. For example

public class ByteArrayComparer : IEqualityComparer<byte[]> {
  public bool Equals(byte[] left, byte[] right) {
    if ( left == null || right == null ) {
      return left == right;
    }
    return left.SequenceEqual(right);
  }
  public int GetHashCode(byte[] key) {
    if (key == null)
      throw new ArgumentNullException("key");
    return key.Sum(b => b);
  }
}

Then you can do

var dict = new Dictionary<byte[], string>(new ByteArrayComparer());

Solution for 2.0

public class ByteArrayComparer : IEqualityComparer<byte[]> {
  public bool Equals(byte[] left, byte[] right) {
    if ( left == null || right == null ) {
      return left == right;
    }
    if ( left.Length != right.Length ) {
      return false;
    }
    for ( int i= 0; i < left.Length; i++) {
      if ( left[i] != right[i] ) {
        return false;
      }
    }
    return true;
  }
  public int GetHashCode(byte[] key) {
    if (key == null)
      throw new ArgumentNullException("key");
    int sum = 0;
    foreach ( byte cur in key ) {
      sum += cur;
    }
    return sum;
  }
}

这篇关于使用字节[]作为字典键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 11:20