我想要做一些性能測量,做與詮釋陣列的一些工作方法,所以我寫了下面的類:在lambda表達式使用操作()的陣列
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);
}
我在做什麼錯了?
?你不能只是調用「測量(動作)」? – GETah
我試過,但我仍然得到'不能隱式地將類型'void'轉換爲'System.Action' –
只是一個簡短的提示,爲什麼你要用ref參數使用void函數,而不是返回一個數組?一般來說,如果你沒有返回任何東西,那麼使用ref參數是不好的做法。在你的情況下,你必須改變你的代碼來使用Func <>而不是Action <>。 –