小编Maa*_*ten的帖子

C# foreach 循环中的奇怪行为

经过一天的故障排除后,我设法将问题浓缩为这一小段代码。有人可以向我解释为什么这不起作用吗?当显示消息框时,我希望 [markets] 为 0 2 4 6,[city] [county] 和 [streets] 为 0 1 2 3。

        private void pieceoftestcode()
        {
            string[] county = new string[4];
            string[] city = new string[4];
            string[] markets = new string[4];
            string[] streets = new string[4];
            string[] items = new string[4] { "apple", "banana", "pineapple", "juice" };
            string[] value = new string[4];
            foreach (string item in items)
            {
                for (int i = 0; i <= 3; i++)
                {
                    if (item == "apple")
                        value[i] = (2 * i).ToString(); …
Run Code Online (Sandbox Code Playgroud)

c# foreach loops

7
推荐指数
1
解决办法
165
查看次数

标签 统计

c# ×1

foreach ×1

loops ×1