Why does List.ForEach allow its list to be modified?

前端 未结 4 1956
礼貌的吻别
礼貌的吻别 2021-01-31 13:02

If I use:

var strings = new List { \"sample\" };
foreach (string s in strings)
{
  Console.WriteLine(s);
  strings.Add(s + \"!\");
}
4条回答
  •  深忆病人
    2021-01-31 13:48

    List.ForEach is implemented through for inside, so it does not use enumerator and it allows to modify the collection.

提交回复
热议问题