We had a discussion at work regarding locking and what exactly happens. The code that triggered this discussion is:
string name = (string)context.Cache[key];
if (String.IsNullOrEmpty(name)){
lock (typeof(string)){
name = (string)context.Cache[key];
//.. other code to get the name and then store in the cache
}
}
I see this as straight-forward: look for a value in the cache, if it's not there then get a lock so as nothing else interrupts whilst the code gets the name and stores it in the cache.
Our discussion focused on whether (typeof(string)) is the best way of doing things, and what exactly is does.
My question is what exactly does lock(typeof(string)) do? Does it create a local string to be used a lock or is it creating something with a wider scope and therefore potentially un-safe.