With the release of .NET 9.0, we have System.Threading.Lock
which brings notable performance benefits over locking on objects, as one needed to do prior to .NET 9.0.
It is therefore recommended to use this going forward, but what if you are ALSO targeting frameworks prior to .NET 9.0?
I started by trying to use preprocessor directives, but it all started to get really messy.