I have the following piece of code:
private Dictionary<object, object> items = new Dictionary<object, object>;
public IEnumerable<object> Keys
{
get
{
foreach (object key in items.Keys)
{
yield return key;
}
}
}
Is this thread-safe? If not do I have to put a lock
around the loop or the yield return
?
Here is what I mean:
Thread1 accesses the Keys
property while Thread2 adds an item to the underlying dictionary. Is Thread1 affected by the add of Thread2?