fork download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. var x = new Int16_2D { a = 1, b = 2 };
  9. var set = new HashSet<Int16_2D> { x };
  10.  
  11. var y = new Int16_2D { a = 1, b = 2 };
  12. Console.WriteLine(set.Contains(y)); // True
  13.  
  14. x.a = 3;
  15. Console.WriteLine(set.Contains(y)); // False
  16. Console.WriteLine(set.Contains(x)); // False!
  17.  
  18. }
  19. }
  20.  
  21. public class Int16_2D
  22. {
  23. public Int16 a, b;
  24.  
  25. public override bool Equals(Object other)
  26. {
  27. return other is Int16_2D &&
  28. a == ((Int16_2D)other).a &&
  29. b == ((Int16_2D)other).b;
  30. }
  31.  
  32. public override int GetHashCode()
  33. {
  34. return a | (int)b * 65536;
  35. }
  36. }
Success #stdin #stdout 0.04s 36960KB
stdin
Standard input is empty
stdout
True
False
False