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?
- x == x
- (object) x == (object) x
- (System.Object) x == (System.Object) x
- (int) (object) x == (int) (object) x
- (float) x == (float) x
- (int) x == (int) x
- (int) x == (float) x
- (float) (int) x == (int) (float) x
- (System.Int32) x == (System.Int32) x
- x.ToString() == x.ToString()
- (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 .