为什么List只包含与currentDate = DateTime.Now.Date相同的日期?

时间:2018-04-10 12:15:48

标签: c# list datetime for-loop

const int IterationsPerDay = 2;
DateTimeOffset currentDate = DateTime.Now.Date;
int dayCount = 0;

for (int j = 0; j < ((IterationsPerDay * 30) / ClientsList.Count); j += IterationsPerDay)
{
    for (int f = 0; f < IterationsPerDay; f++)
    {
        int iterationIndex = j + f;
        if (iterationIndex < productsOfSameCategory.Count)
        {
           Client _client = new Client();
           _client.Name = ClientsList[iterationIndex].Name;
           _client.Date = currentDate;
           VisitsList.Add(_client);
        }
        if (iterationIndex < productsOfSameCategory.Count)
        {
           Client _client = new Client();
           _client.Name = ClientsList[iterationIndex-ClientsList.Count].Name;
           _client.Date = currentDate;
           VisitsList.Add(_client);
        }

    }
    currentDate.AddDays(1);
}

VisitsList应该会添加所有30个日期。每个日期应该有两个客户端,因此列表中应该有60个记录。

为什么循环只添加第一个日期??

1 个答案:

答案 0 :(得分:2)

建议点击相同的帖子:DateTime is immutable

因为日期是结构类型,即值类型及其不可变,你需要这样做

currentDate = currentDate.AddDays(1);
//above line store new data to to your currentdate variable with olddate+1