Quick question.
Take the following code sample:
ArrayList arrayList = new ArrayList();
string testString = "";
testString = "test";
arrayList.Add(testString);
In that code sample, if I were to do the following:
arrayList[0] = "anotherTest";
Would testString be changed to anotherText or would it remain the same? Is it that object that is put in or a copy of it, another string?
Thanks,
Christian
Before:
After:
Only the value of arrayList[0] is changed to a reference to “anotherTest”. The value of the testString variable remains unchanged.
String is a reference type. This means both the testString variable and arrayList[0] each (independently) hold a reference to the objects “test” and “anotherTest”, not the value itself.
Changing testString or arrayList[0] from one string to another string does not modify the original string. Strings are immutable. Only the reference to the string object is changed.