Can anyone tell me if the following code is thread-safe in C#:
ConcurrentBag cb = new ConcurrentBag();
Parallel.ForEach(someCollection, (param1) =>
{`
`cb.Add(GetOutput(param1));
});
private SomeClass GetOutput(InputParameter param1)
{
SomeClass someClassInstance = null;
//declare local variables;
//call an external service;
return someClassInstance;
}
There is no shared state across iterations and the iterations are independent. My doubt is around the GetOutput private method and the local variables declared in it. Will they be allocated separately for each thread? I am 99.99% sure they will be, but wanted to seek expert opinion.
Thanks
Vikas