c# - StringCollection.Count incrementing unexpectedly -


i'm working on method trim each element in stringcollection remove excess in each element don't need. strange when trying method working (it now) noticed when introducing breakpoint in code groupstoclean.count gets incremented each iteration of for loop , can't see why.

does know why?

private stringcollection cleanupgroups(stringcollection groupstoclean) {     stringcollection cleanedgroups = groupstoclean;      (int = 0; < groupstoclean.count; i++)     {         if (groupstoclean[i].indexof(",") != -1)         {             string temp = groupstoclean[i].substring(3, (groupstoclean[i].indexof(",") - 3));             cleanedgroups.add(temp);         }         else             break;     }      return cleanedgroups; } 

thanks insight!

because stringcollection reference type. cleanedgroups , groupstoclean same object, add "cleaned" items collection got them.

to solve problem create new cleanedgroups @ beginning of method:

private stringcollection cleanupgroups(stringcollection groupstoclean) {     var cleanedgroups = new stringcollection(); 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -