C# Lambda 返回语句问题

发布于 2024-10-06 08:34:50 字数 547 浏览 6 评论 0原文

大家好,快速问一下如何调用这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除→记忆 2024-10-13 08:34:50

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 the ret for the first lambda expression is a different ret 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 the ret 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.

灼疼热情 2024-10-13 08:34:50

lambda 是一个匿名函数。因此,在您的情况下, ret 是函数的参数。

当您确实说

Func<int, int> myFunc = (f) => f + 1;

您正在定义一个接受 int 并返回它 + 1 的匿名函数时。您正在 myFunc 引用中存储对匿名函数的引用。这个函数还没有被执行,它只是被定义了。您稍后可以随时调用该函数:

int a = myFunc(4);   // a becomes 5

Action 是一个返回 void 的委托,通常指向一个 lambda(例如您的 lambda)。 Func 是一个委托,它接受一个 T1 并返回一个 T2,并且通常也指向一个 lambda。还为其他需要更多参数的方法定义了其他委托,例如 FuncAction

在您的情况下,您正在处理装饰器两个匿名函数。当它认为有必要时,它会调用它们为它做一些工作,并使用返回值来进一步完成它自己的工作。

A lambda is an anonymous function. So in your case, ret is the parameter to the function.

When you do say

Func<int, int> myFunc = (f) => f + 1;

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:

int a = myFunc(4);   // a becomes 5

An Action<T> is a delegate that returns void and usually points to a lambda such as yours. a Func<T1, T2> is a delegate that takes a T1 and returns a T2, and also usually points to a lambda. There are other delegates defined for other methods that take more parameters such as Func<T1, T2, T3> and Action<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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文