My original code is such like
Object mylock = new object();
void Test()
{
lock(mylock)
{
}
}
Compiled into the following code
void Test
{
object obj = mylock; // add a temporary variable
bool lockTaken = false;
try
{
Monitor.Enter(obj, ref lockTaken);
}
finally
{
if (lockTaken)
{
Monitor.Exit(obj);
}
}
}
I want to know why add a temporary variable rather than use original variable directly as follows.
void Test
{
bool lockTaken = false;
try
{
Monitor.Enter(mylock, ref lockTaken); // use original variable directly
}
finally
{
if (lockTaken)
{
Monitor.Exit(mylock); // use original variable directly
}
}
}
Several C# constructs (using
, lock
) get transformed into final code that way to prevent re-assingnment of the variable used. You really don't want finally
section to act on something different than the value the original operation started with.
Sample of the code that would behave incorrectly if compiler simply uses current value of myLock
in finally
section:
object myLock = new object();
lock(myLock)
{
myLock = new object();
...
}
Note that compiling such code also brings CS0728 warning because attempting to change lock variable will be ignored.