fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var list = new List<Fact> () {
  10. new Fact { Thing = "Roses are", Color = 0xFF0000 },
  11. new Fact { Thing = "Roses are", Color = 0xFF0000 },
  12. new Fact { Thing = "Violets are", Color = 0x0000FF },
  13. new Fact { Thing = "Sugar is", Color = 0x1337 },
  14. new Fact { Thing = "Sugar is", Color = 0x1337 },
  15. };
  16.  
  17. foreach (var item in GetFacts(list)) {
  18. Console.WriteLine ("{0:x} {1:x}", item.Thing, item.Color);
  19. }
  20. }
  21.  
  22. public static IEnumerable<Fact> GetFacts(IEnumerable<Fact> list) {
  23. return list.Distinct();
  24. }
  25.  
  26. }
  27.  
  28.  
  29. public class Fact
  30. {
  31. public string Thing { get; set; }
  32. public int Color { get; set; }
  33.  
  34. public override bool Equals (object obj)
  35. {
  36. if (obj == null) return false;
  37.  
  38. var x = obj as Fact;
  39. if (x == null) return false;
  40.  
  41. return this.Thing == x.Thing && this.Color == x.Color;
  42. }
  43.  
  44.  
  45. public override int GetHashCode ()
  46. {
  47. // http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode
  48. unchecked
  49. {
  50. int hash = 17;
  51. hash = hash * 23 + this.Thing.GetHashCode ();
  52. hash = hash * 23 + this.Color.GetHashCode ();
  53.  
  54. return hash;
  55. }
  56. }
  57. }
Success #stdin #stdout 0.04s 34064KB
stdin
Standard input is empty
stdout
Roses are ff0000
Violets are ff
Sugar is 1337