Let's leave for a moment the parallel part, what you want to do is some opertaion on items in a list of persons, would you choose to use for this purpose for loop:
for(int i = 0; i < persons.Length, i++)
{
persons[i].Name = "SomeName;
}
Or foreach loop:
foreach(Person person in persons)
{
person.Name = "SomeName";
}
I prefer to use the foreach loop in this case because I find it much more suitable and clear, in my opnion that is an obvious choice.
Now lets come back to the parallel part, Parallel.For() should be used to execute for loops in parallel while Parallel.Foreach() should be used to execute foreach loops in parallel.
So if we agreed that foreach loop was more appropriate in this case then we should also prefer Parallel.Foreach() over Parallel.For().
Now for your question Parallel.Foreach() will take an Action of T as an argument, so if you want to pass a method name to the operation it must be void method since Action of T is a delegate to a void method that gets one parameter of type T.
So using your first approach should work:
public void UpdatePersonName(Person person)
{
person.Name = "SomeName";
}
And the parallel part (using this overload):
Parallel.Foreach(persons, UpdatePersonName);
Another option instead of creating seprate methods is to use lambda expressions:
Parallel.Foreach(persons, person => person.Name = "SomeName" );
For this reasons you can't use your second method:
public Person UpdatePersonName(Person person)
{
person.Name = "SomeName";
return person;
}
Inside Parallel.Foreach() because it's return type is not void.
Another importatnt thing you should know, if you are using Parallel.Foreach() you will need to ensure thread safety and most of the times you will need to use some kind of locking to achieve that.
You should consider this carefuly because sometimes it does not worth the overhead, in some cases foreach loops can run faster than their parallel equivalents.