System.Linq.Enumerable comes to the rescue with SequenceEqual and
Use it in the Equals method like so:
bool equal = other._fields.SequenceEqual(_fields);
...
GetHashCode looks something like:
...
Have fun!
Update: Nov 21, 2011
Turns out using Sum is a mistake! There is a high probability that the sum of the hash codes in the collection will exceed int.MAX_VALUE, which will in turn raise an OverflowException! I quick search using my favorite engine lead me to this article where the author provides a few solutions to the problem. I favored using the XOR to compute the composite hash code. The new implementation uses the Aggregate function and looks something like:
int hashCode = _fields.Aggregate(0, (total, field) => total ^ field.GetHashCode());
Update: Nov 21, 2011
Turns out using Sum is a mistake! There is a high probability that the sum of the hash codes in the collection will exceed int.MAX_VALUE, which will in turn raise an OverflowException! I quick search using my favorite engine lead me to this article where the author provides a few solutions to the problem. I favored using the XOR to compute the composite hash code. The new implementation uses the Aggregate function and looks something like:
int hashCode = _fields.Aggregate(0, (total, field) => total ^ field.GetHashCode());
No comments:
Post a Comment