I have code similar to the following:
this.Session[key] = "foo";
if ((this.Session[key] ?? string.Empty) == "foo")
{
//do stuff
}
This, of course, creates a "Possible unintended reference comparison worked as intended" situation. The solution to this is well documented here, and I already knew that the fix was to cast the session variable to a string
as soon as I saw the code.
However, the code is years old and has never been changed since it was originally written. Up until this week in our test environment, the if
statement evaluated as true and executed the //do stuff
section. This erroneous code IS STILL WORKING as intended in our production environment.
How can this be? There's no reason this code as written should have ever worked as intended; and yet it did, and still does in production. And what would have changed to make this code that should not have worked but did, suddenly stop working (or rather, behave like it always should have)?