-3

I don't get why this code's output is :

34
34
List<String[]> list = new List<String[]>();
String[] s = new String[2];

s[0] = "1";
s[1] = "2";

list.Add(s);

s[0] = "3";
s[1] = "4";

list.Add(s);

foreach (String[] match in list)
    Console.WriteLine(match[0] + match[1]);

I think my code is fine to get the output of

12
34

Can someone help?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
remundo
  • 29
  • http://msdn.microsoft.com/en-us/library/s6938f28.aspx – Jeroen Vannevel Jul 17 '14 at 16:39
  • also, http://www.yoda.arachsys.com/csharp/parameters.html – default Jul 17 '14 at 16:40
  • 4
    Because you are printing the same array twice. Arrays are reference types. – afaolek Jul 17 '14 at 16:40
  • ... or [this](http://stackoverflow.com/questions/24775086/none-static-nested-class-inside-static-class-c-sharp) or [this](http://stackoverflow.com/questions/16696448/how-to-make-a-copy-of-an-object-in-c-sharp) or [this](http://stackoverflow.com/questions/78536/deep-cloning-objects) or [this](http://stackoverflow.com/questions/5843958/how-to-make-a-copy-of-a-reference-type) or [this](http://stackoverflow.com/questions/20698361/cloning-a-reference-type) ... – O. R. Mapper Jul 17 '14 at 16:42
  • ... or [this](http://stackoverflow.com/questions/15275251/how-save-array-in-list-by-value-not-reference-in-c-sharp) or [this](http://stackoverflow.com/questions/24157588/objects-in-listt-reference-same-value) or [this](http://stackoverflow.com/questions/22899129/how-to-add-an-array-to-a-list-by-value-not-by-reference) or [this](http://stackoverflow.com/questions/4736947/arraylist-add-adds-just-references) ... – O. R. Mapper Jul 17 '14 at 16:46
  • 4
    When the site asks you to check whether your problem has been discussed before, **please do so**. SO is most useful if all the knowledge on every single problem is gathered in *one* question. – O. R. Mapper Jul 17 '14 at 16:47
  • 4
    Would you expect the output to be "1 2" if you wrote `string s = "1"; s = "2"; Console.WriteLine(s); Console.WriteLine(s);`? – Patrick Jul 17 '14 at 16:54

2 Answers2

3

You are adding the same string array to the list twice. You are only changing the internal values of the array.

You have to initialize a new array to put the new values in:

String[] s = new String[2];

s[0] = "1";
s[1] = "2";

list.Add(s);

s = new String[2]; // initialize new array
s[0] = "3";
s[1] = "4";

list.Add(s);
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
2

An array is a reference type. The list contains the same references as array s. So the both elements of the list are the same references as s. As the last value of s is { "3", "4" } then you get the result as is.

Try the following code

using System;
using System.Collections.Generic;

namespace ListOfArrays
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string[]> list = new List<string[]>();

            list.Add( new string[] { "1", "2" } );
            list.Add( new string[] { "3", "4" } );

            foreach (string[] match in list)
            {
                Console.WriteLine(match[0] + match[1]);
            }
        }
    }
}

The output is

12
34

Take into account that it is better to use type name string instead of String The last is dependent of using directive.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335