我有这样的代码,我正在反复做一些事情,我希望得到完成任务所需的avergae时间。
List<TimeSpan> TimeDisplay = new List<TimeSpan>();
while(some condition)
{
Stopwatch sw = new Stopwatch();
sw.Start();
TimeSpan temp;
//DO SOMETHING\\
sw.Stop();
temp = sw.Elapsed
TimeDisplay.Add(temp);
}
TimeSpan timeaverage = TimeDisplay.Average;
System.Console.Writeline("{0}", timeaverage);
但是我在第二行到第二行时收到错误,说我无法将方法组'Average'转换为'System.TimeSPan'
答案 0 :(得分:5)
试试这个:
TimeSpan timeaverage = TimeSpan.FromMilliseconds(TimeDisplay.Average(i=>i.TotalMilliseconds));
答案 1 :(得分:1)
TimeSpan timeaverage = TimeSpan.FromTicks(Convert.ToInt64(TimeDisplay.Average(ts => ts.Ticks)));