它,我想創造一種推廣方法是這樣的:Dictionarry <T,List<U>> Extention線程安全法「AddToList」是創建一個列表,如果不存在
public static void AddToList<T,U>(
this Dictionary<T,List<U>> dictionary, T key, U value)
{
//If the list exist, add to the list.
//Else Create the list and add the item.
}
這是我試過到目前爲止:
public static void AddToList<T,U>(this Dictionary<T,List<U>> dictionary, T key, U value)
{
if (!dictionary.ContainsKey(key) || dictionary[key] == null)
{
dictionary[key] = new List<U>();
}
dictionary[key].Add(value);
}
我該如何處理線程安全?
要鎖定迪科的訪問,以便圍繞AddList的通話 – Thomas 2014-10-01 13:36:52