I see that the two links refer to objects with equal properties, but this is not what is tested here. It checks whether they are references to the same object or that they are equal. Your StatResult
class StatResult
not override Equals
/ GetHashCode
, so two objects with the same values will be considered "different" for testing.
You must override Equals
and GetHashCode
so that the two objects are considered equal, respectively. I also suggest making the type immutable, and also following the normal .NET naming conventions for properties:
public sealed class StatResult : IEquatable<StatResult> { public string Word { get; private set; } public UInt64 Views { get; private set; } public StatResult(string word, ulong views) { this.word = word; this.views = views; } public override string ToString() { return String.Format("{0};{1}", word, views); } public override int GetHashCode() { int hash = 23; hash = hash * 31 + Word == null ? 0 : Word.GetHashCode(); hash = hash * 31 + Views.GetHashCode(); return hash; } public override bool Equals(object other) { return Equals(other as StatResult); } public bool Equals(StatResult other) { return other != null && this.Word == other.Word && this.Views == other.Views; } }
Your design will simply change to:
new StatResult("test", 98318), new StatResult("test board", 7801),
(as well as in your production code).
source share