The answer provided by Leonardo Muzzi
(this one)[
^] is correct.
My advice adds to this: Therefore, most universal simple way to lock data on some type's instance is to declare non-static private lock object:
private object LockObject = new object();
lock (this.LockObject) {}
There can be cases when some portions of data your data class are completely independent, so you don't need to interlock between them, but need to lock each portion. Then, do it separately, by having as many lock objects as the number of those independent portions of data.
Finally,
lock
construct itself in by far not a the general case. There are very typical cases when you need different access when you read data from your data type instance or you write to it. In particular, it is very usual that there are many reads, much less writes. In this case, use
System.Threading.ReaderWriterLockSlim
(see Microsoft documentation on the topic). To target .NET Framework v.2.0, another (presently obsolete) type is needed:
System.Threading.ReaderWriterLock
, but performance is not as good.