It seems very strange to return different objects every time ... in fact, I very rarely (if ever) used the SyncRoot approach, since often I need to synchronize between multiple objects, etc., so a separate object is more significant.
But if the data is really immutable (read-only), why not just return false from IsSynchronized?
Re GC - any such object is usually short-lived and assembled in GEN0. If you have a field with an object (for blocking), it will continue until the collection, but most likely it does not hurt ...
If you need a lock, Iโll be tempted to just:
private readonly object lockObj = new object();
You can also use the lazy approach only to the โnewโ when necessary, which makes some sense if you really don't expect anyone to ask for synchronization (by returning false to IsSynchronized).
Another common approach is to return "this"; it keeps things simple, but the risks conflict with some other code, using your object as a lock for an unrelated purpose. Rare, but possible. This is actually the approach that [MethodImpl] uses for synchronization.
Marc gravell
source share