class Program
{
static Action act = null;
static void Main(string[] args)
{
Test();
act();
act();
Test();
act();
}
static void Test()
{
int count = 0;
if(act == null) act = () => Console.Write(++count + " ");
}
}
Run Code Online (Sandbox Code Playgroud)
结果:1 2 3为什么?
如果delete[ if(act == null)]
结果:1 2 1
两次过载之间的时间差List<T>.Sort(...)。
List<int> list = new List<int>();
Random rand = new Random();
for (int i = 0; i < 20_000_000; i++)
{
list.Add(rand.Next());
}
Stopwatch watch = new Stopwatch();
watch.Start();
//list.Sort(); // 1.77 sec
list.Sort((n1, n2) => n1.CompareTo(n2)); // 5.80 sec
watch.Stop();
Console.WriteLine(watch.Elapsed.TotalSeconds);
Run Code Online (Sandbox Code Playgroud)
为什么第二种形式这么慢?