Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

like image 500
chum of chance Avatar asked Aug 27 '10 02:08

chum of chance


2 Answers

TimeSpans are value types. Try:

totalTime = totalTime.Add(time)

like image 69
Jamezor Avatar answered Oct 14 '22 07:10

Jamezor


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

like image 38
Bradley Smith Avatar answered Oct 14 '22 05:10

Bradley Smith