Skip to content

Lock Class

Stands in for System.Threading.Lock, which arrived in .NET 9.

C#
public sealed class Lock

Inheritance System.Object → Lock

Remarks

Lets a single source file use the newer mutual-exclusion type across every target this library supports. On .NET 9 and later the real type is used, and the compiler lowers lock on it to EnterScope(); on .NET 8 this stands in and the same statement lowers to Monitor, which is what the code would have used anyway.

The shape mirrors the real type only as far as lock needs, because that is all this exists for: nothing should take a dependency on a polyfill beyond the syntax it enables.

Methods

Lock.EnterScope() Method

Enters the lock, returning a scope that leaves it when disposed.

C#
public System.Threading.Lock.Scope EnterScope();

Returns

Scope