An extension method to safely retrieve or add a dictionary value The extension method:

/// &lt;summary&gt;
/// Thread safe. May lock on the input dictionary.
/// &lt;/summary&gt;
public static U GetOrAdd&lt;T, U&gt;(this Dictionary&lt;T, U&gt; dict, T key, Func&lt;U&gt; create) {
    U val;
    if (!dict.TryGetValue(key, out val)) {
        lock (dict) {
            if (!dict.ContainsKey(key)) {
                val = create();
                dict[key] = val;
            } else {
                val = dict[key];
            }
        }
    }
    return val;
}


Sample usage:

var idVals = new Dictionary&lt;int, List&lt;int&gt;&gt;();
List&lt;int&gt; vals = idVals.GetOrAdd(id, () =&gt; new List&lt;int&gt;());
vals.Add(3);


Explanation:


If the key exists, we quickly retrieve the value using the
TryGetValue() method
If the key doesn't exist we use the provided create delegate to make a new one
We are protected against the case of two threads simultaneously looking for a key that doesn't exist by locking on the input
dictionary and afterward re-checking to see if the key still doesn't exist


Questions:


Are there scenarios that will cause this fail? 
I found this extension method to be useful, but is clear enough to be properly understood by others? 
Are there easier ways to achieve the thread-safety I'm looking for? 
I know it's considered dangerous and bad practice to lock on an input parameter, but can I rely on the fact that locking on a dictionary to safely add/remove elements is a common convention?
Are there easier ways to achieve the thread-safety I'm looking for? 


Yes, use the ConcurrentDictionary Class which is avaible since NET 4.0