Recently I came across code as following:
void CallThisInDifferentThreads(Return return)
{
var lock = "lock";
lock(lock)
{
//Do something here.
}
}
My first reaction was that the lock in this code won't work because we are creating the lock and using it in the same method. Every Thread calling this method has it's own copy of lock so there will be no synchronisation.
But later I realized that this should work because string goes to string pool and there is only one instance of a particular string. I'm not sure If I'm correct.