Using lock(obj) inside a recursive call
- by Amby
As per my understanding a lock is not released until the runtime completes the code block of the lock(obj) ( because when the block completes it calls Monitor.Exit(obj).
With this understanding i am not able to understand the reason behind the behaviour of the following code.
private static string obj = "";
private static void RecurseSome(int number)
{
Console.WriteLine(number);
lock (obj)
{
RecurseSome(++number);
}
}
//Call: RecurseSome(0)
//Output: 0 1 2 3...... stack overflow exception
There must be some concept that i am missing. Please help.