C# List Manipulation - Output Different When Stepping Through vs. Running
<p>I'm a C# newbie and I'm really confused about something I'm trying to do for a project in a C# class.</p>

<p>The assignment is some list manipulation in C#.</p>

<p>The program accepts a list of items in the text box, then iterates through those items, creating multiple copies of the list. It randomly resizes each copy of the list to between 3 and all items. It then outputs all the copies.</p>

<p>The problem I'm having is that when I step through this program with the debugger, I get the expected output. The same happens if I display a message box after each iteration (as I have in the code below).</p>

<p>However, if I just run the program straight through, I get a different output. Instead of variations in the lists, all the copies of the list are exactly the same.</p>

<p>If you see in the code I've commented "// FIRST DEBUG MESSAGEBOX" and "// SECOND DEBUG MESSAGEBOX". If the first debug messagebox code is left in there, the output is as expected...multiple versions of the list are output with random lengths between 3 and all items.</p>

<p>However, and this is where I'm confused...if you comment out the first debug messagebox code, you get a different result. All versions of the list output are the same length with no variation.</p>

<p>Any help would be appreciated! Here's the code I have so far...sorry if it's terrible - I'm new at C#:</p>

<pre><code>public partial class MainForm : Form
{
    /**
     * Vars to hold raw text list items
     * and list items split by line
     */
    String rawListItems = "";
    List&lt;string&gt; listItems = new List&lt;string&gt;();
    List&lt;List&lt;string&gt;&gt; textListItems = new List&lt;List&lt;string&gt;&gt;();

    public MainForm()
    {
        InitializeComponent();
    }

    private void cmdGo_Click(object sender, EventArgs e)
    {
        // store the contents of the list item text box
        this.rawListItems = txtListItems.Text;
        this.listItems.AddRange(Regex.Split(this.rawListItems, "\r\n"));

        // setup min and max items - max items all items
        int minItems = 3;
        int maxItems = this.listItems.Count;

        // We'll copy this list X times, X = same number of items in list
        for (int i = 0; i &lt; this.listItems.Count; i++)
        {
            // make a copy of the list items
            List&lt;string&gt; listItemsCopy = new List&lt;string&gt;(this.listItems);

            // get a random number between min items and max items
            Random random = new Random();
            int maxIndex = random.Next(minItems, maxItems + 1);    // max is exclusive, hence the +1

            // remove all elements after the maxIndex
            for (int j = 0; j &lt; listItemsCopy.Count; j++)
            {
                if (j &gt; maxIndex)
                {
                    listItemsCopy.RemoveAt(j);
                }
            }

            // add the list copy to the master list
            this.textListItems.Add(listItemsCopy);

            // FIRST DEBUG MESSAGEBOX
            String tst = "";
            foreach (string item in listItemsCopy)
            {
                tst += item + " ## ";
            }
            MessageBox.Show(tst);
        }

        // SECOND DEBUG MESSAGEBOX
        String output = "";
        foreach (List&lt;string&gt; listitem in this.textListItems)
        {
            foreach (string item in listitem)
            {
                output += item + " ## ";
            }
        }
        MessageBox.Show(output);
    }
}
</code></pre>
