Class Item gets empty when List.Clear() using c# -


this class declaration:

    list<carton> cartons = new list<carton>();     public class carton     {         public string cartonlabel;         public string pn;         public list<string> serials;                 } 

this generic list use @ end assign carton.serials

    list<string> serials = new list<string>(); 

why carton.serials become empty when do: serials.clear()

    if (serials.count > 0)                         {                             carton cartonitem = new carton();                             cartonitem.cartonlabel = cartondata;                             cartonitem.pn = pndata;                             cartonitem.serials = serials;                             cartons.add(cartonitem);                             serials.clear();                         } 

you assign serials cartonitem.serials. doesn't create copy. it's still same list, both variables point same instance.

if want create copy, can call tolist():

cartonitem.serials = serials.tolist(); 

make sure add using system.linq; top of *.cs file.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -