From other questions I can see that locking on types is a bad idea. But it is possible to do so, so I was wondering if it is such a bad thing to do why is it allowed? I am assuming there must be good use cases for its purpose so could someone let me know what they are please?
Share
To understand why it is a bad idea in general have a look at the article Don’t lock type objects.
It is allowed because the language/framework designers decided to be able to take lock on anything that derives from
System.Object. Nobody can prevent it becauseSystem.Typederives fromSystem.Object(as every other .NET type).Take this signature:
How could a compiler enforce that
ois noSystem.Type? You could of course check it at runtime, but this would have a performance impact.And of course there might be super-exotic situations where one might need to lock on a type. Maybe the CLR does it internally.