You do not determine which local variable in Parallel.ForEach - item is nothing more than a formal parameter - the implementation code of Parallel.ForEach is the one that will process the variables and whether they will be local, captured, or something else.
There is no need to define a local variable associated with the formal parameter Parallel.ForEach - the caller code of your anonymous delegate processes the variable and passes it to your function.
However, in C # 4, you may need to use a local variable if you capture another variable, namely:
void DoSomething(ItemType item, OtherType other) { } void YourFunction(IEnumerable<ItemType> items, IEnumerable<OtherType> others) { foreach (var otherItem in others) { var localOtherItem = otherItem; Parallel.ForEach(items, item => DoSomething(item, localOtherItem)); } }
You can see the difference above: localOtherItem is taken from the context where the anonymous function is defined: this is called a closure. While items in items are passed simply as a parameter to a method of an anonymous function.
In short: item in Parallel.ForEach and item in C # foreach are two very different problems.
Jean hominal
source share