如何扩展码表类?
public static class StopwatchExtensions
{
public static long Time(this Stopwatch sw, Action action, int iterations)
{
sw.Reset();
sw.Start();
for (int i = 0; i < iterations; i++)
{
action();
}
sw.Stop();
return sw.ElapsedMilliseconds;
}
}
然后这样称呼它:
var s = new Stopwatch();
Console.WriteLine(s.Time(() => DoStuff(), 1000));