title: Dictionary<T,List<U>> extension thread-safe method 'AddToList' that create a list if it does not exist
body: <p>I want to create an extension method like this :</p>
  
  <pre><code>public static void AddToList&lt;T,U&gt;(this Dictionary&lt;T,List&lt;U&gt;&gt; dictionary, T key, U value)
  {
      // If the list exist, add to the list.
      // Else Create the list and add the item.
  }
  </code></pre>
  
  <p>This is what I tried so far :</p>
  
  <pre><code>public static void AddToList&lt;T,U&gt;(this Dictionary&lt;T,List&lt;U&gt;&gt; dictionary, T key, U value)
  {
      if (!dictionary.ContainsKey(key) || dictionary[key] == null)
      {
          dictionary[key] = new List&lt;U&gt;();
      }
      dictionary[key].Add(value);
  }
  </code></pre>
  
  <p>How could I handle thread safety with this method?</p>