Question : What value does the second lock statment provide?

Does the lock(_locker) inside Main do anything useful?

If so, what?

newbieweb
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
static readonly object _locker = new object();
 
static void Main()
{
  lock (_locker)
  {
     AnotherMethod();
     // We still have the lock - because locks are reentrant.
  }
}
 
static void AnotherMethod()
{
  lock (_locker) { Console.WriteLine ("Another method"); }
}

Answer : What value does the second lock statment provide?

I guess the only one thing that the code author is trying to prove that locks can be nested, so in main you start a locking on _locker object, then call AnotherMethod(), inside there starts a new locking on the same _locker object, ends that second locking and when going back to Main a locking is still active for _locker object, the one that established in Main originally.
Random Solutions  
 
programming4us programming4us