Why won't a Hashtable return true for “ContainsKey” for a key of type byte[] in C#?

前端 未结 4 1453
一个人的身影
一个人的身影 2021-01-22 13:13

Consider the following code:

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

Hashtable ht = new Hashtable();
ht.Add(         


        
4条回答
  •  一向
    一向 (楼主)
    2021-01-22 13:44

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

    Yes it should. Arrays are reference types.

    Everything is working as it's supposed to.

    If you want different behaviour, you can implement a comparator for arrays that compares the contents and pass that to the hashtable.

提交回复
热议问题