我想对使用 int 数组的方法进行一些性能测量,所以我编写了以下类:
public class TimeKeeper
{
public TimeSpan Measure(Action[] actions)
{
var watch = new Stopwatch();
watch.Start();
foreach (var action in actions)
{
action();
}
return watch.Elapsed;
}
}
但我不能Measure
为下面的例子调用 mehotd:
var elpased = new TimeKeeper();
elpased.Measure(
() =>
new Action[]
{
FillArray(ref a, "a", 10000),
FillArray(ref a, "a", 10000),
FillArray(ref a, "a", 10000)
});
我收到以下错误:
Cannot convert lambda expression to type 'System.Action[]' because it is not a delegate type
Cannot implicitly convert type 'void' to 'System.Action'
Cannot implicitly convert type 'void' to 'System.Action'
Cannot implicitly convert type 'void' to 'System.Action'
这是适用于数组的方法:
private void FillArray(ref int[] array, string name, int count)
{
array = new int[count];
for (int i = 0; i < array.Length; i++)
{
array[i] = i;
}
Console.WriteLine("Array {0} is now filled up with {1} values", name, count);
}
我做错了什么?