对于两个列表中的每个元素

Elm*_*lmo 1 .net c# arrays foreach list

这是我的代码:

List<string> l1 = new List<string> { "a", "b", "c" };
List<string> l2 = new List<string> { "x", "y", "z" };
foreach (var item in l1)
{
    item = MyFunction(item);
}
foreach (var item in l1)
{
    item = MyFunction(item);
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在单个foreach语句中迭代这两个列表?

Ser*_*rvy 7

假设List您可以使用的对象类型相同Concat.

foreach (var item in l1.Concat(l2))
{
    item = MyFunction(item);
}
Run Code Online (Sandbox Code Playgroud)