fork(1) download
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. public class Test
  7. {
  8. public class Foo
  9. {
  10. public int ID { get; set; }
  11.  
  12. public override int GetHashCode()
  13. {
  14. Console.WriteLine("in GetHashCode, ID=" + ID);
  15. return ID.GetHashCode();
  16. }
  17. public override bool Equals(object obj)
  18. {
  19. Console.WriteLine("in Equals, ID=" + ID);
  20. Foo f2 = obj as Foo;
  21. if (f2 == null) return false;
  22. return ID == f2.ID;
  23. }
  24. }
  25.  
  26. public static void Main()
  27. {
  28. IEnumerable<Foo> list1 = new List<Foo>() { new Foo { ID = 1 }, new Foo { ID = 2 }, new Foo { ID = 3 } };
  29. IEnumerable<Foo> list2 = new List<Foo>() { new Foo { ID = 2 }, new Foo { ID = 3}, new Foo { ID = 4 } };
  30. Console.WriteLine("before Intersect");
  31. IEnumerable<Foo> inBoth = list1.Intersect(list2);
  32. Console.WriteLine("after Intersect");
  33.  
  34. foreach (Foo fDup in inBoth)
  35. {
  36. // now GetHashCode will be executed
  37. Console.WriteLine("in foreach, ID=" + fDup.ID);
  38. }
  39. }
  40. }
Success #stdin #stdout 0.04s 33992KB
stdin
Standard input is empty
stdout
before Intersect
after Intersect
in GetHashCode, ID=2
in GetHashCode, ID=3
in GetHashCode, ID=4
in GetHashCode, ID=1
in GetHashCode, ID=2
in Equals, ID=2
in foreach, ID=2
in GetHashCode, ID=3
in Equals, ID=3
in foreach, ID=3