views:

39

answers:

3

I have stumbled across some code that is adding strings to a List but hashing the value before adding it.

It's using an MD5 hash (MD5CryptoServiceProvider) on the string value, then adding this to the list.

Is there any value in doing this in terms of speed to find the key in the list or is this just unnecessary?

A: 

I don't think it's necessary to do this for a List if the aim is to improve performance. Strings are strings and are looked up the same way whether they are hashed or not.

Bernard
+1  A: 

I am not going to assume to know what the authors of the code you were viewing were doing with their list. But I will say that if you have a large list and performance of searching is critical, then there's a class for that. HashSet<T> will suit your needs nicely.

Anthony Pegram
A: 

First of all, a list (List<T>) does not have “keys”. However, a Dictionary<TKey, TValue> does.

Secondly, to answer your performance question: no, there is actually a performance penalty in computing that hash. However, before you jump to the conclusion that it is unnecessary, examine the surrounding code and think about whether the author may have actually needed the MD5 hashsum and not the string itself?

Thirdly, if you need to look something up efficiently, you can use a HashSet<T> if you just need to check its existence, or Dictionary<TKey, TValue> if you need to associate the keys that you look up with a value.

If you place strings in a dictionary or hashset, C# will already generate a hash value from any string you put in. This generally uses a hash algorithm that is much faster than MD5.

Timwi