I'm using Visual Studio's Watch panel to investigate variables as I run my program. In the following code, both test1
and test2
variables are added to the Watch panel. As expected, they both evaluate to true
.
object a = "123";
object b = "123";
bool test1 = a == b;
bool test2 = (object)"123" == (object)"123";
However, as seen by the third line, if I manually add the expression for the test2
variable, it evaluates to false
.
Why the same line yields different result in the Watch window? Is it some kind of optimization in work here? Under what circumstances can the same string be allocated to different addresses?