What does () => mean in C#?

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

    It's a lambda expression. The most common syntax is using a parameter, so there are no parentheses needed around it:

    n => Times.AtLeast(n)
    

    If the number of parameters is something other than one, parentheses are needed:

    (n, m) => Times.AtLeast(n + m)
    

    When there are zero parameters, you get the somewhat awkward syntax with the parentheses around the empty parameter list:

    () => Times.AtLeast(0)
    

提交回复
热议问题