using System;
using System.Linq;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
var list = new List<Fact> () {
new Fact { Thing = "Roses are", Color = 0xFF0000 },
new Fact { Thing = "Roses are", Color = 0xFF0000 },
new Fact { Thing = "Violets are", Color = 0x0000FF },
new Fact { Thing = "Sugar is", Color = 0x1337 },
new Fact { Thing = "Sugar is", Color = 0x1337 },
};
foreach (var item in GetFacts(list)) {
Console.WriteLine ("{0:x} {1:x}", item.Thing, item.Color);
}
}
public static IEnumerable<Fact> GetFacts(IEnumerable<Fact> list) {
return list.Distinct();
}
}
public class Fact
{
public string Thing { get; set; }
public int Color { get; set; }
public override bool Equals (object obj)
{
if (obj == null) return false;
var x = obj as Fact;
if (x == null) return false;
return this.Thing == x.Thing && this.Color == x.Color;
}
public override int GetHashCode ()
{
// http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode
unchecked
{
int hash = 17;
hash = hash * 23 + this.Thing.GetHashCode ();
hash = hash * 23 + this.Color.GetHashCode ();
return hash;
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uTGlucTsKdXNpbmcgU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWM7CiAKcHVibGljIGNsYXNzIFRlc3QKewogICAgICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBNYWluKCkKICAgICAgICB7CiAgICAgICAgICAgICAgICB2YXIgbGlzdCA9IG5ldyBMaXN0PEZhY3Q+ICgpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG5ldyBGYWN0IHsgVGhpbmcgPSAiUm9zZXMgYXJlIiwgQ29sb3IgPSAweEZGMDAwMCB9LAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBuZXcgRmFjdCB7IFRoaW5nID0gIlJvc2VzIGFyZSIsIENvbG9yID0gMHhGRjAwMDAgfSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbmV3IEZhY3QgeyBUaGluZyA9ICJWaW9sZXRzIGFyZSIsIENvbG9yID0gMHgwMDAwRkYgfSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbmV3IEZhY3QgeyBUaGluZyA9ICJTdWdhciBpcyIsIENvbG9yID0gMHgxMzM3IH0sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG5ldyBGYWN0IHsgVGhpbmcgPSAiU3VnYXIgaXMiLCBDb2xvciA9IDB4MTMzNyB9LAogICAgICAgICAgICAgICAgICAgICAgICAgICAgfTsKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgZm9yZWFjaCAodmFyIGl0ZW0gaW4gR2V0RmFjdHMobGlzdCkpIHsKICAgICAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSAoInswOnh9IHsxOnh9IiwgaXRlbS5UaGluZywgaXRlbS5Db2xvcik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIAogICAgICAgIHB1YmxpYyBzdGF0aWMgSUVudW1lcmFibGU8RmFjdD4gR2V0RmFjdHMoSUVudW1lcmFibGU8RmFjdD4gbGlzdCkgewogICAgICAgICAgICByZXR1cm4gbGlzdC5EaXN0aW5jdCgpOwogICAgICAgIH0KIAp9CiAKIApwdWJsaWMgY2xhc3MgRmFjdAp7CiAgICBwdWJsaWMgc3RyaW5nIFRoaW5nIHsgZ2V0OyBzZXQ7IH0KICAgIHB1YmxpYyBpbnQgQ29sb3IgeyBnZXQ7IHNldDsgfQogICAgCiAgICBwdWJsaWMgb3ZlcnJpZGUgYm9vbCBFcXVhbHMgKG9iamVjdCBvYmopCiAgICB7CiAgICAgICAgaWYgKG9iaiA9PSBudWxsKSByZXR1cm4gZmFsc2U7CiAgICAgICAgCiAgICAgICAgdmFyIHggPSBvYmogYXMgRmFjdDsKICAgICAgICBpZiAoeCA9PSBudWxsKSByZXR1cm4gIGZhbHNlOwogICAgICAgIAogICAgICAgIHJldHVybiB0aGlzLlRoaW5nID09IHguVGhpbmcgJiYg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