What does () => mean in C#?

前端 未结 6 1354
再見小時候
再見小時候 2020-12-06 06:20

I\'ve been reading through the source code for Moq and I came across the following unit test:

Assert.Throws(() => Times         


        
6条回答
  •  星月不相逢
    2020-12-06 07:01

    Search StackOverflow for "lambda".

    Specifically:

    () => Console.WriteLine("Hi!");
    

    That means "a method that takes no arguments and returns void, and when you call it, it writes the message to the console."

    You can store it in an Action variable:

    Action a = () => Console.WriteLine("Hi!");
    

    And then you can call it:

    a();
    

提交回复
热议问题