Why can't we assign a foreach iteration variable, whereas we can completely modify it with an accessor?

前端 未结 9 523
情歌与酒
情歌与酒 2020-11-28 08:13

I was just curious about this: the following code will not compile, because we cannot modify a foreach iteration variable:

        foreach (var item in MyObj         


        
9条回答
  •  清酒与你
    2020-11-28 08:51

    Use for loop instead of foreach loop and assign value. it will work

    foreach (var a in FixValue)
    {
        for (int i = 0; i < str.Count(); i++)
        {
           if (a.Value.Contains(str[i]))
               str[i] = a.Key;
        }
     }
    

提交回复
热议问题