What is the best way to modify a list in a 'foreach' loop?

前端 未结 11 850
不思量自难忘°
不思量自难忘° 2020-11-22 05:23

A new feature in C# / .NET 4.0 is that you can change your enumerable in a foreach without getting the exception. See Paul Jackson\'s blog entry An Interest

11条回答
  •  不知归路
    2020-11-22 06:14

    I have written one easy step, but because of this performance will be degraded

    Here is my code snippet:-

    for (int tempReg = 0; tempReg < reg.Matches(lines).Count; tempReg++)
                                {
                                    foreach (Match match in reg.Matches(lines))
                                    {
                                        var aStringBuilder = new StringBuilder(lines);
                                        aStringBuilder.Insert(startIndex, match.ToString().Replace(",", " ");
                                        lines[k] = aStringBuilder.ToString();
                                        tempReg = 0;
                                        break;
                                    }
                                }
    

提交回复
热议问题