C# 中 Python 的 functools.partial 的等价物是什么?

发布于 2025-01-09 04:14:49 字数 405 浏览 3 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(1

迷雾森÷林ヴ 2025-01-16 04:14:49

根据您的示例,我能想到的最好的接近是 函数委托

这提供了一种基于几乎任何东西定义局部函数的方法。在本例中,它是在本地 Main 范围中定义的。

用法:

using System;
                    
public class Program
{
    public static void Main()
    {
       // Func<,> type: input is 'int', output will be 'int'
       // 'c' = input argument.
       // '=>' indicates what will be done with it.  
       Func<int,int> add_to_one = (c) => Add(c,1);

       //call 'add_to_one' as a normal method.
       int result = add_to_one(2);
       
       Console.WriteLine(result);
    }
    
    //example method
    public static int Add(int a, int b)
    {
        return a + b;
    }
}

输出:

3

对于一般情况:

返回一个新的部分对象,该对象在调用时的行为类似于使用位置参数 args 和关键字参数 keywords 调用的 func 。如果向调用提供更多参数,它们将附加到 args 中。

这部分内容已涵盖。您将拥有一个行为类似于方法/函数的可调用变量。

至于这个:

如果提供了其他关键字参数,它们会扩展并覆盖关键字。

该结构有可能扩展到更多的输入变量,例如:

// 3 inputs
// output is string
Func<int,string,double,string> foo = (i,s,d) => $"int: {i}, string: {s}, double {d}";

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:

using System;
                    
public class Program
{
    public static void Main()
    {
       // Func<,> type: input is 'int', output will be 'int'
       // 'c' = input argument.
       // '=>' indicates what will be done with it.  
       Func<int,int> add_to_one = (c) => Add(c,1);

       //call 'add_to_one' as a normal method.
       int result = add_to_one(2);
       
       Console.WriteLine(result);
    }
    
    //example method
    public static int Add(int a, int b)
    {
        return a + b;
    }
}

Output:

3

As for the general case:

Return a new partial object which when called will behave like func called with the positional arguments args and keyword arguments keywords. If more arguments are supplied to the call, they are appended to args.

This part is covered. You'll have a callable variable behaving like a method/function.

As for this:

If additional keyword arguments are supplied, they extend and override keywords.

It is possible, the construction is expandable to many more input variables, e.g.:

// 3 inputs
// output is string
Func<int,string,double,string> foo = (i,s,d) => 
quot;int: {i}, string: {s}, double {d}";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文