Is there any difference between this:
internal class MyClass
{
private readonly object _syncRoot = new Object();
public void DoSomething()
{
The first method is preferred because you can (and should) make _syncRoot
private. This lowers the risk of deadlocking.
The MethodImplOptions.Synchronized
is a left-over from an earlier ambitious idea that turned out to be not so good after all.
Regarding the last question: Yes, according to this blog they are functionally equivalent (but not implemented the same way). And all forms of lock(this)
are discouraged, again because of deadlock scenarios.