将列表转换为数组的意外结果

时间:2015-04-21 05:39:25

标签: c# arrays date

以下代码输出" 2月,2月,2月,2月,2月和#34;

但是我试图在滚动的基础上输出过去6个月 - 有人可以帮忙吗?

谢谢!

      DateTime EndDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);//The AddDays function is self explanatory although you can have negative numbers in here

          DateTime StartDate = EndDate.AddMonths(-1); //When do you want the meetings to start DateTime.Today will return midnight
          DateTime m = StartDate.AddMonths(-6);

          List<months> monthstore = new List<months>();

          months monthdata = new months();

          while (m < StartDate)
          {
              monthdata.month = m.ToString("MMMM");
              monthstore.Add(monthdata);

              m = m.AddMonths(1);
          }
        var publicationTable = new[] {
    new[] { monthstore.Select(x=> x.month).ToArray()  }

};

1 个答案:

答案 0 :(得分:3)

 months monthdata = new months();

进入循环,你总是在改变同一个对象。 months是引用类型