c# - Clone or Duplicate the first object and Insert to the same list -
i have list of phones came database, , clone first entry , insert same list (updated entry).
list<phones> p = new list<phones> { new phones { id = 1, phone = "samsung s3" }, new phones { id = 2, phone = "iphone 5" }, new phones { id = 3, phone = "samsung s4" }, new phones { id = 4, phone = "samsung s5" }, new phones { id = 5, phone = "iphone 5s" }, new phones { id = 6, phone = "iphone 6" }, };
currently used below approach. first entry updates also.
var obj = p.firstordefault(); var t = new phones(); t = obj; t.id = 7; p.add(t); public class phones { public int id { get; set; } public string phone { get; set; } public bool ischild { get; set; } }
class call reference if change value changing first member well. can clone phone, should implement iclonable interface, consider link implementing iclonable
public class phones : icloneable { public int id { get; set; } public string phone { get; set; } public bool ischild { get; set; } public object clone() { return new phones() { id = this.id, phone = this.phone, ischild = this.ischild }; } }
Comments
Post a Comment