()=>是什么?在C#中意味着什么? [英] What does () => mean in C#?
问题描述
我一直在阅读 Moq 的源代码,以下单元测试:
I've been reading through the source code for Moq and I came across the following unit test:
Assert.Throws<ArgumentOutOfRangeException>(() => Times.AtLeast(0));
在我的一生中,我不记得()=>的实际作用。我认为这与匿名方法或Lambda有关。而且我确定我知道它的作用,此刻我不记得了...。
And for the life of me, I can't remember what () => actually does. I'm think it has something to do with anonymous methods or lambdas. And I'm sure I know what it does, I just can't remember at the moment....
并使情况更糟.... Google并没有太大帮助和 stackoverflow都不是
And to make matters worse....google isn't being much help and neither is stackoverflow
有人可以给我一个非常愚蠢的问题的快速答案吗?
Can someone give me a quick answer to a pretty noobish question?
推荐答案
()=>是一个无效的lambda表达式。它表示传递给assert的匿名函数。抛出,并在该函数内部调用。
()=> is a nullary lambda expression. it represents an anonymous function that's passed to assert.Throws, and is called somewhere inside of that function.
void DoThisTwice(Action a) {
a();
a();
}
Action printHello = () => Console.Write("Hello ");
DoThisTwice(printHello);
// prints "Hello Hello "
这篇关于()=>是什么?在C#中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!