Just to make sure, say that i have this code:
this.allObjects = [some linq query];
and have two methods that both read (not modify) this IEnumerable, are they safe to call in parallel? Just looping through a IEnumerable should be safe right?
Just to make sure, say that i have this code:
this.allObjects = [some linq query];
and have two methods that both read (not modify) this IEnumerable, are they safe to call in parallel? Just looping through a IEnumerable should be safe right?
It depends on the implementation of the Enumerator, but in most cases, yes, it is safe.
One note though: the pseudocode, as you have it, doesn't actually do anything (due to deferred execution). The LINQ query will only execute when you access allObjects
(or if you call something like ToList()
on the LINQ query).
The IEnumerable<T>
instances are probably fine (although it does depend on implementation).
IEnumerator<T>
instances are probably not fine (although again: implementation details).