Pergunta

I am looking for a quick way to find if hashtable (string to byte array) collection values contains a given value, i can't use the given contains as it will compare the array object not its values.

i don't mind using linq / extension / implementing contains as long as it is short.

i have tried:

byte[] givenArr = new[]{1,2,3,4}; //(a given arr)
bool contains=false;
Hashtable table;
foreach(var val in table.values)
  if (CompareBytesFunction((byte[])val,givenArr))
      contains=true;

where compare bytes is a function to compare the 2 given byte arrays, i feel this is not the right approach. there might be a simpler way to get this without the helper method.

Foi útil?

Solução

Even though it's pretty much against the concept of Hashtables which should be searched by key, you can use such code:

//suppose this is the byte array you're looking for:
byte[] b = new byte[] { 1, 3, 5 };

bool exists = myHashtable.Values.OfType<byte[]>().
    ToList().Exists(c => c.SequenceEqual(b));

Outras dicas

You should probably implement an IEqualityComparer that can hash and compare byte[]. Then you can just use the normal hashtable methods.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top