try-catch. , , , , , , Dictionary. , - Dictionary , , . .
1: .NET 4.0, ConcurrentDictionary.
2: .NET 3.5, Reactive Extensions backport. ConcurrentDictionary System.Threading.dll.
3: , Dictionary. , , . , "" Dictionary, .
public class Example
{
private readonly Dictionary<int, CustomObject> official = new Dictionary<int, CustomObject>();
private volatile Dictionary<int, CustomObject> copy = new Dictionary<int, CustomObject>();
public class Example()
{
}
public void Set(int id, CustomObject value)
{
lock (official)
{
official[id] = value;
var clone = new Dictionary<int, CustomObject>();
foreach (var kvp in official)
{
clone.Add(kvp.Key, kvp.Value);
}
copy = clone;
}
}
public CustomObject Get(int id)
{
CustomObject value = null;
if (copy.TryGetValue(id, out value))
{
return value;
}
return null;
}
}
, Dictionary . , .
4: , lock.