private object lockObj = new object();
private Dictionary dict = new Dictionary();
public string GetOrAddFromDict(int key)
{
a) This is not thread-safe, as the underlying Dictionary
itself is not thread safe. If another thread is calling Add at the same time, undefined behavior can occur.
b) This is effectively an attempt at double-checked locking.
I would recommend using the ConcurrentDictionary class instead, as it's designed for this scenario. Another option would be to use a ReaderWriterLockSlim (or ReaderWriterLock), if you're not targetting .NET 4.0.