首先我将我的班级添加到列表中然后将该类复制到新类中,并根据逻辑更改日期后再次将其保存在list 中,但已经保存在列表中的第一个元素的日期也会被更改。  吼叫  我完整的代码 if (Session["CompanyId"] != null) { List<CalendarList> CalendartableList = new List<CalendarList>(); DateTime cldate = Convert.ToDateTime(CalendarList.DateDue); CalendarList.CompanyId = Convert.ToInt32(Session["CompanyId"]); CalendarList.Category = 15; CalendarList.Status = 1; CalendarList.Identifier = 232; CalendarList.Quarter = 5; CalendarList.IsCompleted = false; CalendarList.CalendarGUID = CalendarList.Description; CalendartableList.Add(CalendarList); DateTime po_Date = cldate; int pi_Count = 0; while (po_Date.Year == DateTime.Now.Year) { pi_Count += 1; switch (CalendarList.Interval) { case "Weekly": { po_Date = cldate.AddDays(7 * pi_Count); break; } case "Biweekly": { po_Date = cldate.AddDays(14 * pi_Count); break; } case "Monthly": { po_Date = cldate.AddMonths(1 * pi_Count); break; } default: { po_Date = cldate.AddMonths(3 * pi_Count); break; } } if (po_Date.Year == DateTime.Now.Year) { CalendarList newlist = new CalendarList(); newlist = CalendarList; newlist.DateDue = po_Date.ToString(); CalendartableList.Add(newlist); } }  还有其他方法可以达到相同目的吗? (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 您需要记住的是,您正在向reference添加一个object,而不是该对象的副本。最简单的解决方法是在您的.Clone()中添加object方法以获取其自身的副本并将其创建为新的object。可以独立于原始副本修改克隆的副本。关于c# - 如何在list <>中添加一个类并再次复制该类,并进行一些修改后再次添加,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57204169/ (adsbygoogle = window.adsbygoogle || []).push({});
10-09 04:48