在 C# 中使用带有 stringArray 的“foreach”循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16512161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:11:35  来源:igfitidea点击:

Using a 'foreach' loop with stringArray in C#

c#arrays

提问by user2371290

I am writing a program which should display the items from an array in a foreach loop.

我正在编写一个程序,它应该在 foreach 循环中显示数组中的项目。

I wanted to change the elements of the array by adding a string "sad" to each element, but when run the program the array stays the same.

我想通过向每个元素添加一个字符串“sad”来更改数组的元素,但是当运行程序时,数组保持不变。

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] stringArray = {"hey", "Tom"};

            for (int i = 0; i < stringArray.Length; i++ )
            {
                stringArray[i] += "  dad";
                Console.WriteLine(stringArray[i]);
            }

            Array.Resize(ref stringArray, stringArray.Length + 1);

            // Add bob to the last element of the array
            stringArray[stringArray.Length - 1] =" bob";

            foreach (string s in stringArray)
            {
                string b = s + "sad";
                Console.WriteLine(s);
                //Console.WriteLine(stringArray);
            }
        }
    }
}

回答by christopher

string b = s + "sad";

Console.WriteLine(s);

//Console.WriteLine(stringArray);

At no point in your code do you alter values in the array. You create a new stringfrom each value in the array, concatenated with the string "sad".

在您的代码中,您绝不会更改数组中的值。您string从数组中的每个值创建一个新值,并与字符串连接"sad"

Solution

解决方案

You can not alter a for-eachvariable. You'll get a message like:

你不能改变一个for-each变量。您会收到如下消息:

Cannot assign to 's' because it is a 'foreach iteration variable'.

无法分配给“s”,因为它是“foreach 迭代变量”。

Instead, settle for a simple forloop.

相反,满足于一个简单的for循环。

for(int x = 0; x < stringArray.length; x++)
{
     stringArray[x] = stringArray[x] + "sad";
}

回答by Guffa

Look at this part of the code:

看这部分代码:

string b = s + "sad";
Console.WriteLine(s);

You are concatenating the string in swith the string "sad", and storing in the variable b. Then you display the content of the variable s. If you would display the content of the variable bisntead, there would be a sadat the end of each string.

您将字符串s与字符串连接起来"sad",并存储在变量中b。然后显示变量的内容s。如果要显示变量bistead的内容,则sad每个字符串的末尾都会有一个。

回答by Marc Gravell

        foreach (string s in stringArray)
        {   
            string b = s + "sad";
            // ...
        }

Here you are creating a new string, completely unrelated to the string in the string-array. You haven't changed the old string (you can't; strings are immutable). You then simply drop this new longer string on the floor - you aren't updating the array etc.

在这里,您正在创建一个新字符串,与字符串数组中的字符串完全无关。您没有更改旧字符串(您不能更改;字符串是不可变的)。然后您只需将这个新的更长的字符串放在地板上 - 您没有更新数组等。

Try instead something like:

尝试改为:

for(int i = 0 ; i < stringArray.Length ; i++)
{
    stringArray[i] = stringArray[i] + "sad";
}

This replacesevery item in the array with a new string. Note that you can't update a list/collection/array etc while iterating with foreach- that can break the iterator. Hence the forloop instead.

用新字符串替换数组中的每个项目。请注意,您不能在迭代时更新列表/集合/数组等foreach- 这可能会破坏迭代器。因此,for循环代替。

回答by Tim Schmelter

Apart from what Chris said, you could simply use LINQ to achieve what you want:

除了克里斯所说的,您可以简单地使用 LINQ 来实现您想要的:

string[] newStringArray = stringArray
    .Select(s => s + "sad")
    .ToArray();