What does () => mean in C#?

前端 未结 6 1350
再見小時候
再見小時候 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:08

    That's the definition of a lambda (anonymous) function. Essentially, it's a way to define a function inline, since Assert.Throws takes a function as an argument and attempts to run it (and then verify that it throws a certain exception).

    Essentially, the snippet you have there is a unit test that makes sure Times.AtLeast(0) throws a ArgumentOutOfRangeException. The lambda function is necessary (instead of just trying to call the Times.AtLeast function directly from Assert.Throws) in order to pass the proper argument for the test - in this case 0.

    MSDN KB article on the topic here: http://msdn.microsoft.com/en-us/library/bb882516.aspx

提交回复
热议问题