c# - Why list show strange behaviour? -
work on vs2010 ef,c#.
have 2 list(olistranitem,olisttaxitem) ,need copy 1 list properties values in list ,then need work on new list.problem after copy content element 1 list list type of changes impact on both list ,why happen ,i change on list changes occur in both list please check bellow syntax.
list<transactionitem> olistranitem = new list<transactionitem>(); list<transactionitem> olisttaxitem = new list<transactionitem>(); olistranitem = _transactionitem; olisttaxitem = _transactionitemtax; transactionitem tmpitem = new transactionitem(); tmpitem = olistranitem.where(item => item.quotationdetailid == quotationdetailid && item.action != entity.actionmode.delete && item.isdeleted == false).firstordefault(); if (tmpitem.isnotnull()) { tmpitem.action = entity.actionmode.add; olisttaxitem.add(tmpitem); } else { _transactionitemtax = new list<transactionitem>(); } int ncounter = 5; foreach (transactionitem item in olisttaxitem) { if (item.quotationtaxid ==0) { ncounter = ncounter + 1; item.quotationtaxid = ncounter; } }
please me identify why problem aries,how solve problem.
if have query please ask,thanks in advanced.
transactionitem class, right? , not struct.
every type that's class is, default, reference type. means have in lists not real values of transaction items, references (think c++ pointers) values. when copy data 1 list other, you're copying references.
you need clone items 1 list another. give class method clone instances, , use method copy items 1 list another.
Comments
Post a Comment