I have the following code:
string[] firstArray = { "bla bla", "bla bla", "bla bla"};
string[] secondArray = {"aaaaaaaaaaaa", "aaaaaaaaaaa", "aaaaaaaaa"};
string[] newArray = firstArray;
firstArray = secondArray;
foreach (string item in newArray)
{
Console.WriteLine(item);
}
This code gives the following results:
bla bla
bla bla
bla bla
I can’t understand why the newArray has the same conntent after I assign a different instance to the firstArray. Please help me.
You’re assigning references to arrays, not the arrays themselves.
firstArray has a reference to the blabla array.
secondArray has a reference to the aaaaaaaaaaaa array.
I guess there was a
array = firstArraysomewhere. newArray holds a reference to the same array as firstArray (blabla).array holds a reference to the same array as secondArray (aaaaaaaaaaaa).
newArray still holds a reference to the blabla array, so that prints.