.net - data mismatch in array's C# when both are made equal -
i declared array like
string[] arr1; string[] arr2;
in 1 point assigend value
arr1 = new string[] { "value1" , "value2 }; arr2 = arr1;
after again changing value of arr1
arr1[0]="value3"; arr1[1]="value4";
now if check arr2 these changes in arr1 reflects.
arr2[0] value "value3"; arr2[1] value "value4";
how happening ?
arrays reference types in .net. when did arr2 = arr1
made both variables point same array in memory. change elements of 1 directly reflected in other.
to make copy of array, use array.clone
method:
arr2 = (string[])arr1.clone();
or linq's enumerable.toarray
method:
arr2 = arr1.toarray();
Comments
Post a Comment