Strange List<string[]> behaviour...
-
Good morning, I have been searching for a long time, until I nailed this behaviour:
public static void Test()
{
string[] strArray = { "", ""};
List strArrayList = new List();strArray\[0\] = "A"; strArrayList.Add(strArray); strArray\[0\] = "B"; strArrayList.Add(strArray); strArray\[0\] = "C"; strArrayList.Add(strArray); MessageBox.Show("strArrayList: " + strArrayList\[0\]\[0\] + strArrayList\[1\]\[0\] + strArrayList\[2\]\[0\]); // I expect: "ABC", but it shows: "CCC"! string str = ""; List strList = new List(); str = "A"; strList.Add(str); str = "B"; strList.Add(str); str = "C"; strList.Add(str); MessageBox.Show("strList: " + strList\[0\] + strList\[1\] + strList\[2\]); // I expect: "ABC", and it shows: "ABC"! }
When I change a string and then add it to a list, everything is fine. However, when I change a string in a string array and then add this array to a list, all previously stored arrays in the list are updated as well! Is this normal or am I doing something wrong? Of course, there is this ridiculous work-around "strArrayList.Add(strArray.ToList().ToArray());" Which works! :-D Thanks in advance, Olof
-
Good morning, I have been searching for a long time, until I nailed this behaviour:
public static void Test()
{
string[] strArray = { "", ""};
List strArrayList = new List();strArray\[0\] = "A"; strArrayList.Add(strArray); strArray\[0\] = "B"; strArrayList.Add(strArray); strArray\[0\] = "C"; strArrayList.Add(strArray); MessageBox.Show("strArrayList: " + strArrayList\[0\]\[0\] + strArrayList\[1\]\[0\] + strArrayList\[2\]\[0\]); // I expect: "ABC", but it shows: "CCC"! string str = ""; List strList = new List(); str = "A"; strList.Add(str); str = "B"; strList.Add(str); str = "C"; strList.Add(str); MessageBox.Show("strList: " + strList\[0\] + strList\[1\] + strList\[2\]); // I expect: "ABC", and it shows: "ABC"! }
When I change a string and then add it to a list, everything is fine. However, when I change a string in a string array and then add this array to a list, all previously stored arrays in the list are updated as well! Is this normal or am I doing something wrong? Of course, there is this ridiculous work-around "strArrayList.Add(strArray.ToList().ToArray());" Which works! :-D Thanks in advance, Olof
You have to take into account that strArray is a reference type. It seems that when you add it to strArrayList, a reference is added. Then, strArrayList contains a list of references to string arrays, which are the same array since you have been reusing the variable without reasignating it. This is similar to what happens in the following case:
public void Test() {
string[] a = new string[] {"", ""};
string[] b = a;
a[0] = "A";
b[0] = "B";
Console.WriteLine("{0} {1}", a[0], b[0]);
} -
You have to take into account that strArray is a reference type. It seems that when you add it to strArrayList, a reference is added. Then, strArrayList contains a list of references to string arrays, which are the same array since you have been reusing the variable without reasignating it. This is similar to what happens in the following case:
public void Test() {
string[] a = new string[] {"", ""};
string[] b = a;
a[0] = "A";
b[0] = "B";
Console.WriteLine("{0} {1}", a[0], b[0]);
}Thank you, Olof