C# 中 Python 的 functools.partial 的等价物是什么?
在 python 中,我们有一个 functools.partial
< /a> 机制。有效的 C# 等效项是什么?
对于那些不熟悉这个概念的人,我正在寻找一般情况,但它可以是任何东西,甚至:
def add(a: int, b: int):
return a + b
add_to_one = partial(add, 1)
add_to_one(2) # This will return 3
In python we have a functools.partial
mechanism. What would be a valid C# equivalent?
For those unfamiliar with the concept, I am looking for the general case, but it could be anything, even:
def add(a: int, b: int):
return a + b
add_to_one = partial(add, 1)
add_to_one(2) # This will return 3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的示例,我能想到的最好的接近是 函数委托。
这提供了一种基于几乎任何东西定义局部函数的方法。在本例中,它是在本地
Main
范围中定义的。用法:
输出:
对于一般情况:
这部分内容已涵盖。您将拥有一个行为类似于方法/函数的可调用变量。
至于这个:
该结构有可能扩展到更多的输入变量,例如:
Based on your example, the best I can think of that comes close, is a Func delegate.
This provides a method of defining a local function, based on pretty much anything. In this case it's defined in the local
Main
scope.Usage:
Output:
As for the general case:
This part is covered. You'll have a callable variable behaving like a method/function.
As for this:
It is possible, the construction is expandable to many more input variables, e.g.: