asp.net - "List.Remove" in C# does not remove item? -
hello how can remove item generic list here code im trying right dont know make mistake;/
users us_end = new users(); foreach (var variable in ((list<users>)application["users_on"])) { if(variable.id == (int)session["current_id"]) { us_end.name = variable.name; us_end.id = variable.id; us_end.data = variable.data; } } list<users> = ((list<users>)application["users_on"]); us.remove(us_end); application["users_on"] = us;
you have same object remove, not copy.
users us_end; foreach (var variable in ((list<users>)application["users_on"])) { if(variable.id == (int)session["current_id"]) { us_end = (users)variable; break; } } if (us_end != null) { list<users> = ((list<users>)application["users_on"]); us.remove(us_end); application["users_on"] = us; } edit:
just clarify address here, pst pointed, implement iequatable interface , overridings on groo's answer make work, think it's overkill on specific subject. giving common practice, making clear it's possible remove items list, if diferent instances or diferent objects technique that.
Comments
Post a Comment