C# Lambda 返回语句问题
大家好,快速问一下如何调用这个 lambda 或者它实际上做了什么。
public Composite CreateBuffCheckAndCast(string name, UnitSelectDelegate onUnit, CanRunDecoratorDelegate extra)
{
return new Decorator(
ret => Spells.CanBuff(name, onUnit(ret)) && extra(ret),
new Action(ret => Spells.Buff(name, onUnit(ret))));
}
不幸的是,我没有这个类的其余部分,并且已经有一段时间没有使用 Lambda 了。那个“ret”变量从哪里来?调用函数?它是否用于获取 IEnumerable 我可以看到编译器将其分配给 onUnit 接受的任何类型..?
解:
ret =>用于将 Spells.CanBuff 转换为装饰器接受的委托类型。 onUnit 还将接受委托函数参数。
Hey all, quick question on how to call this lambda or what it actually does..
public Composite CreateBuffCheckAndCast(string name, UnitSelectDelegate onUnit, CanRunDecoratorDelegate extra)
{
return new Decorator(
ret => Spells.CanBuff(name, onUnit(ret)) && extra(ret),
new Action(ret => Spells.Buff(name, onUnit(ret))));
}
Unfortunately don't have the rest of this class and haven't used Lambdas in a while.. Where does that "ret" variable come from? The calling function? Is it used to grab an IEnumerable I could see the compiler assigning it whatever type onUnit would accept..?
Solution:
ret => is used to transform Spells.CanBuff into a delegate type to be accepted by Decorator. The onUnit would also accept a delegate function parameter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ret 变量是 lambda 表达式正在构建的委托(或表达式树)的参数。请注意,第一个 lambda 表达式的
ret
与第二个 lambda 表达式中的ret
不同。因此,创建了两个委托,并将它们传递给
Decorator
构造函数,该构造函数可能存储它们以供稍后执行。调用每个委托时,调用者必须传递一个值,该值在 lambda 表达式执行期间可用作 ret 参数。如果没有看到
Decorator
构造函数的签名是什么,就很难说更多了。恐怕我不太确定你的第二段代码的相关性是什么。
The
ret
variable is the parameter to the delegate (or expression tree) that the lambda expression is building. Note that theret
for the first lambda expression is a differentret
to the one in the second lambda expression.So, two delegates are created, and they are passed to the
Decorator
constructor, which presumably stores them to execute them later on. When each delegate is called, the caller will have to pass a value in which will be available as theret
parameter during the lambda expression's execution.Without seeing what the signature of the
Decorator
constructor is, it's hard to say any more than that.I'm not quite sure what the relevance of your second snippet of code is, I'm afraid.
lambda 是一个匿名函数。因此,在您的情况下, ret 是函数的参数。
当您确实说
您正在定义一个接受 int 并返回它 + 1 的匿名函数时。您正在
myFunc
引用中存储对匿名函数的引用。这个函数还没有被执行,它只是被定义了。您稍后可以随时调用该函数:Action
是一个返回 void 的委托,通常指向一个 lambda(例如您的 lambda)。Func
是一个委托,它接受一个T1
并返回一个T2
,并且通常也指向一个 lambda。还为其他需要更多参数的方法定义了其他委托,例如Func
和Action
在您的情况下,您正在处理装饰器两个匿名函数。当它认为有必要时,它会调用它们为它做一些工作,并使用返回值来进一步完成它自己的工作。
A lambda is an anonymous function. So in your case,
ret
is the parameter to the function.When you do say
You are defining an anonymous function that takes an int and returns it + 1. You are storing a reference to the anonymous function in the
myFunc
reference. This function has not executed, it's just been defined. You can later call the function whenever you like:An
Action<T>
is a delegate that returns void and usually points to a lambda such as yours. aFunc<T1, T2>
is a delegate that takes aT1
and returns aT2
, and also usually points to a lambda. There are other delegates defined for other methods that take more parameters such asFunc<T1, T2, T3>
andAction<T1, T2>
In your case you are handing the Decorator two anonymous functions. When it deems necessary, it will call them to do some work for it, and use the return value to further its own work.