问题
I have two timespans and I want to add the second timespan to the first timespan:
TimeSpan weeklyWorkTimeHours = new TimeSpan(0,0,0);
TimeSpan? completeWorkTimeForCurrentDay =
CalculateCompleteWorktime(currentWorkTimeItem).Value; /* I debugged through
the code. This method returns a correct timespan with a correct value */
weeklyWorkTimeHours.Add(completeWorkTimeForCurrentDay.Value);
But even after the last line of code, weeklyWorkTimeHours contains 0,0,0. Why doesn't add work in this context?
回答1:
The return value is a new TimeSpan
, the original TimeSpan
is not modified.
Try this:
weeklyWorkTimeHours = weeklyWorkTimeHours.Add(completeWorkTimeForCurrentDay.Value);
来源:https://stackoverflow.com/questions/28041852/add-timespan-to-another-timespan-does-not-work