18

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

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.

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);
            }
        }
    }
}
2
  • You should change string b = s + "sad"; to string s += "sad"; Commented May 12, 2013 at 21:25
  • possible typo Console.WriteLine(b); instead of Console.WriteLine(s); Commented May 12, 2013 at 21:28

4 Answers 4

15
        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 replaces every 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 for loop instead.

Sign up to request clarification or add additional context in comments.

Comments

13

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

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

Comments

4
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 string from each value in the array, concatenated with the string "sad".

Solution

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

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

Instead, settle for a simple for loop.

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

Comments

0

Look at this part of the code:

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

You are concatenating the string in s with 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 b isntead, there would be a sad at the end of each string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.