1.

What is the lock statement in C#?

Answer»

During multi-threading, when a thread is inside a critical section, it must not be removed or stopped until it is completed. To implement this feature, lock statements are used in C# which prevents other threads from entering a locked code (currently in the critical section), until the object is released.

static object obj = new object();

public static void LockDemo()
{
lock(obj)
{
Console.WriteLine("Demonstrating use of Lock statement in C#");
}
}


Discussion

No Comment Found