c# - List with references don't let me continue -
c# - List with references don't let me continue -
i'm doing c# programme , have problem when modify element of list<> menor
, list<> mayor
value alter list<> menor
, here code. give thanks you.
list<string[]> listas(list<string[]> mayor, list<string[]> menor) { list<string[]> may = new list<string[]>(mayor);//here clone list mayor list<string[]> men = new list<string[]>(menor);//here clone list menor string[] var_aux = null; ; (int = 0; i<mayor.count;i++ ) { if (men.find(delegate(string[] s) { homecoming s[0] == may.elementat(i)[0]; })==null)//here find similar elements { var_aux = new string[4]; var_aux = may.elementat(i); var_aux[3] = "0";//here alter de element[3] men.add(var_aux);//and here element changed in men, alter elements in may how can avoid this? } } men.sort((s, t) => string.compare(s[0], t[0])); homecoming men; }
this:
var_aux = new string[4];
is never used, next lines replaces reference array in may
:
var_aux = may.elementat(i);
you modify contents of array:
var_aux[3] = "0";
var_aux
same array exists in may
. if want copy, need clone array, example:
var_aux = may.elementat(i).toarray();
c# list reference
Comments
Post a Comment