I have a function in C# that can be called multiple times from multiple threads and I want it to be done only once so I thought about this:
class MyClass {
The lock keyword is just syntactic sugar for Monitor.Enter and Monitor.Exit:
lock
Monitor.Enter(o); try { //put your code here } finally { Monitor.Exit(o); }
is the same as
lock(o) { //put your code here }