Why isn't my TimeSpan.Add() working?

狂风中的少年 提交于 2019-12-17 19:07:52

问题


There has to be an easy answer:

var totalTime = TimeSpan.Zero;

foreach (var timesheet in timeSheets)
{
   //assume "time" is a correct, positive TimeSpan
   var time = timesheet.EndTime - timesheet.StartTime;
   totalTime.Add(time);
}

There's only one value in the list timeSheets and it is a positive TimeSpan (verified on local inspection).


回答1:


TimeSpans are value types. Try:

totalTime = totalTime.Add(time)




回答2:


This is a common mistake. TimeSpan.Add returns a new instance of TimeSpan.




回答3:


totalTime = totalTime.Add(time)


来源:https://stackoverflow.com/questions/3580947/why-isnt-my-timespan-add-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!