Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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 <code>GetHashCode()</code>; the correct behaviour is that no warnings are printed when you run it... switch the comments to fix it:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; static class Program { static void Main() { // first and second are logically equivalent SimpleTableRow&lt;int&gt; first = new SimpleTableRow&lt;int&gt;(1, 2, 3, 4, 5, 6), second = new SimpleTableRow&lt;int&gt;(1, 2, 3, 4, 5, 6); if (first.Equals(second) &amp;&amp; first.GetHashCode() != second.GetHashCode()) { // proven Equals, but GetHashCode() disagrees Console.WriteLine("We have a problem"); } HashSet&lt;SimpleTableRow&lt;int&gt;&gt; set = new HashSet&lt;SimpleTableRow&lt;int&gt;&gt;(); set.Add(first); set.Add(second); // which confuses anything that uses hash algorithms if (set.Count != 1) Console.WriteLine("Yup, very bad indeed"); } } class SimpleTableRow&lt;T&gt; : IEquatable&lt;SimpleTableRow&lt;T&gt;&gt; { public SimpleTableRow(int id, params T[] values) { this.Id = id; this.Values = values; } public int Id { get; private set; } public T[] Values { get; private set; } public override int GetHashCode() // wrong { return Id.GetHashCode() ^ Values.GetHashCode(); } /* public override int GetHashCode() // right { int hash = Id; if (Values != null) { hash = (hash * 17) + Values.Length; foreach (T t in Values) { hash *= 17; if (t != null) hash = hash + t.GetHashCode(); } } return hash; } */ public override bool Equals(object obj) { return Equals(obj as SimpleTableRow&lt;T&gt;); } public bool Equals(SimpleTableRow&lt;T&gt; 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 &amp;&amp; Values.SequenceEqual(other.Values); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload