<p>I am re-factoring some code and am wondering about the use of a <code>lock</code> in the instance constructor.</p>

<pre><code>public class MyClass {

    private static Int32 counter = 0;
    private Int32 myCount;

    public MyClass() {

        lock(this) {
            counter++;
            myCount = counter;
        }
    }
}
</code></pre>

<p>Please confirm</p>

<ol>
<li>Instance constructors are thread-safe.</li>
<li>The lock statement prevents access to that code block, not to the static 'counter' member.</li>
</ol>

<p>If the intent of the original programmer were to have each instance know its 'count', how would I synchronize access to the 'counter' member to ensure that another thread isn't new'ing a <code>MyClass</code> and changing the count before this one sets its count?</p>

<p><em>FYI - This class is not a singleton. Instances must simply be aware of their number.</em></p>

I'm guessing this is for a singleton pattern or something like it. What you want to do is not lock your object, but lock the counter while your are modifying it.
```
```
private static int counter = 0;
private static object counterLock = new Object();

lock(counterLock) {
    counter++;
    myCounter = counter;
}
```
```
Because your current code is sort of redundant. Especially being in the constructor where there is only one thread that can call a constructor, unlike with methods where it could be shared across threads and be accessed from any thread that is shared.
From the little I can tell from you code, you are trying to give the object the current count at the time of it being created. So with the above code the counter will be locked while the counter is updated and set locally. So all other constructors will have to wait for the counter to be released.