Here’s a silly little C# pop-quiz about equality. Given a local variable x declared of type ‘int’, what do the following C# expressions evaluate to?

  1. x == x
  2. (object) x == (object) x
  3. (System.Object) x == (System.Object) x
  4. (int) (object) x == (int) (object) x
  5. (float) x == (float) x
  6. (int) x == (int) x
  7. (int) x == (float) x
  8. (float) (int) x == (int) (float) x
  9. (System.Int32) x == (System.Int32) x
  10. x.ToString() == x.ToString()
  11. (object) x.ToString() == (object) x.ToString()

For bonus points, identify any additional work in each expression that’s not directly comparing equality of two integers.

 

[Update] To simplify, assume an unchecked context (which is the default). I've posted the answers here

[Update] If you think changing operator== to Object.Equals will solve all your problems, try this related quiz here .