I am trying to optimize my code, I want to call Parallel.For inside a Parallel.ForEach(). I am not sure how can I do so. If I do see that the results are not correct. My code reverses the word within a sentence.
changed that for loop and it works.
for (int i = word.Length - 1; i >= 0; i--)
Here is my original code which fails.
public string Test()
{
string s = "Hello how are you";
if (s.Length > 0)
{
StringBuilder reverseS = new StringBuilder(s.Length);
string[] words = s.Split(' ');
Parallel.ForEach(words, word =>
{
StringBuilder builder = new StringBuilder(word.Length);
Parallel.For(0, word.Length - 1, i =>
//for (int i = word.Length - 1; i >= 0; i--)
{
builder.Append(word[i]);
});
reverseS.Append(builder);
reverseS.Append(" ");
});
return reverseS.ToString();
}
else
{
return "";
}
}
olleH woh era uoy