- Whether you put the return statement inside or outside a lock statement, It doesn’t make any difference; they’re both translated to the same thing by the compiler.
void example()
{
int testData;
lock (foo)
{
testData = ...;
}
return testData
- It is recommended to put the return inside the lock. Otherwise you risk another thread entering the lock and modifying your variable before the return statement, therefore making the original caller receive a different value than expected.
void example()
{
lock (foo)
{
return ...;
}
}
No comments