I have a class that contains the following two properties:
public int Id { get; private set; } public T[] Values { get; private set; }
I have made it IEquatable<T> and overriden the object.Equals like this:
public override bool Equals(object obj) { return Equals(obj as SimpleTableRow<T>); } public bool Equals(SimpleTableRow<T> other) { // Check for null if(ReferenceEquals(other, null)) return false; // Check for same reference if(ReferenceEquals(this, other)) return true; // Check for same Id and same Values return Id == other.Id && Values.SequenceEqual(other.Values); }
When having override object.Equals I must also override GetHashCode of course. But what code should I implement? How do I create a hashcode out of a generic array? And how do I combine it with the Id integer?
public override int GetHashCode() { return // What? }
Because of the problems raised in this thread, I’m posting another reply showing what happens if you get it wrong… mainly, that you can’t use the array’s
GetHashCode(); the correct behaviour is that no warnings are printed when you run it… switch the comments to fix it: