I need to be able to enumerate the collection in any order (foreach) and Add/Remove an item from any thread.
Direct access by index is not needed.
What collection do I use? Simply a List? If so, do I only have to lock on add/remove or do I have to lock on foreach also?
I want the following operations to be available on any thread.
foreach (var item in myCollection)
{
// myCollection can be returned in any order.
}
myCollection.Add(item)
var success = myCollection.Remove(item) // returns false if the item does not exist in myCollection