C#中async/await真的是大大简化了异步程序的编写,但往往我们可能忽略了异步匿名委托和Lambda表达式。

直接上代码:

class Program
{
    static void Main(string[] args)
    {
        Func<Task> asyncFunc1 = async delegate
        {
            Console.WriteLine("Begin in Anonymous Delegate...");
            await Task.Delay(TimeSpan.FromSeconds(2));
            Console.WriteLine("End in Anonymous Delegate...");
        };
 
        Func<Task> asyncFunc2 = async () =>
        {
            Console.WriteLine("Begin in Lambda...");
            await Task.Delay(TimeSpan.FromSeconds(2));
            Console.WriteLine("End in Lambda...");
        };
 
        asyncFunc1();
        asyncFunc2();
 
        Console.WriteLine("Main thread");
        Console.ReadKey();
    }
}

输出:

C#中异步匿名委托和Lambda表达式

原来异步匿名委托或者异步Lambda表达式也是很容易编写的。

相关文章:

  • 2019-08-21
  • 2021-10-08
  • 2020-06-16
  • 2019-01-18
  • 2021-10-11
  • 2021-08-09
  • 2018-06-24
  • 2021-09-08
猜你喜欢
  • 2020-07-25
  • 2021-11-23
  • 2021-07-04
  • 2020-03-15
  • 2021-09-27
  • 2021-08-02
  • 2020-04-14
  • 2017-11-30
相关资源
相似解决方案