zoukankan      html  css  js  c++  java
  • Dictionary<byte[],string> 出现错误的解决方案

    今天在使用 Hbase 的Thrift接口的时候, C# 生成的代码出现错误为, “给定的关键字不在字典中”

    image

    经google 后找到答案, 原地址为: http://stackoverflow.com/questions/1440392/use-byte-as-key-in-dictionary

    下面是我的测试代码,原代码中 有一个错误  “ <> “ 要改为“!=” 就好了。

    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;
          }
      }
       

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

      //      return  key.GetHashCode();
      //        //return key.Sum();
      //    }
      //}

      class Program
      {

          static void Main(string[] args)
          {

              Dictionary<byte[], string> dic = new Dictionary<byte[], string>(new ByteArrayComparer());
              dic.Add("key".ToBytes(), "zbbb");
              Console.WriteLine(dic["key".ToBytes()]);
    }

    }

    注意 gethashcode 可能会有问题,先放这里,随后再看。

  • 相关阅读:
    Selector + 线程池 遇到的问题
    【转】Android TabActivity无法正常bindService解决方法
    Android 中的 Service 全面总结
    【转】IT 圈里有哪些经常被读错的词?
    【转】线程的7种状态及相互转换
    【eoeandroid 特刊】第117期打包网盘下载地址
    使用 Android 自带的 proguard 混淆源码
    Google+ 连接不上的解决办法
    【转】AsyncTask的用法
    winForm简单数据绑定
  • 原文地址:https://www.cnblogs.com/zbw911/p/2270841.html
Copyright © 2011-2022 走看看