我有这样的代码,我重复做某事,我想获得完成该任务所需的平均时间。
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”
最佳答案
试试这个:
TimeSpan timeaverage = TimeSpan.FromMilliseconds(TimeDisplay.Average(i=>i.TotalMilliseconds));
关于c# - 时间跨度平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17682169/