gpt4 book ai didi

c#-3.0 - () => 在 C# 中是什么意思?

转载 作者:行者123 更新时间:2023-12-04 02:32:46 25 4
gpt4 key购买 nike

我一直在阅读 Moq 的源代码我遇到了以下单元测试:

Assert.Throws<ArgumentOutOfRangeException>(() => Times.AtLeast(0));

对于我的生活,我不记得 () => 实际上做了什么。我认为这与匿名方法或 lambdas 有关。而且我确定我知道它是做什么的,我现在想不起来了……

更糟糕的是...... google isn't being much helpneither is stackoverflow

有人可以给我一个非常菜的问题的快速答案吗?

最佳答案

()=> 是一个空的 lambda 表达式。它代表一个传递给 assert.Throws 的匿名函数,并在该函数内部的某处被调用。

void DoThisTwice(Action a) { 
a();
a();
}
Action printHello = () => Console.Write("Hello ");
DoThisTwice(printHello);

// prints "Hello Hello "

关于c#-3.0 - () => 在 C# 中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1216004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com