Have a look at the following code:
static void Main(string[] args)
{
string s = null;
string[] myArray = new string[1];
{ } // do something evil here
if (s.GetType() == typeof(int))
{
Console.WriteLine("This should not happen!");
}
Console.ReadLine();
}
Is there any way to get This should not happen
to write? One would assume not. However, it can be done with the debugger: Put a breakpoint into the line { } // do something evil here
and execute the following commands in the Immediate Window before continuing:
((object[])myArray)[0] = 99;
s = myArray[0];
Execution continues and This should not happen
will be printed. Tested with Visual Studio 2008; here is a screenshot:
Is this kind of trickery only possible with the debugger or is there some way to do such an "unsafe assignment" in code?
(Obviously, I ask only out of scientific curiosity. This question and the related comments made me ask this question.)