Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法C的参考资料#_C#_Python_Functional Programming - Fatal编程技术网

C# 方法C的参考资料#

C# 方法C的参考资料#,c#,python,functional-programming,C#,Python,Functional Programming,我只是想知道这个python代码是否有C#等价物。 我想将方法的名称存储在某种集合中,稍后再调用它们。我已经找过了,但我真的不知道该找什么 例如,在python中,我可以执行以下操作: def add_one(x): return x + 1 def double_it(x): return x*2 maths_rules = [add_one, double_it] def do_maths(maths_rules, x): for func in maths_rules:

我只是想知道这个python代码是否有C#等价物。 我想将方法的名称存储在某种集合中,稍后再调用它们。我已经找过了,但我真的不知道该找什么

例如,在python中,我可以执行以下操作:

def add_one(x):
  return x + 1
def double_it(x):
  return x*2

maths_rules = [add_one, double_it]
def do_maths(maths_rules, x):
  for func in maths_rules:
    x = func(x)
  return x

print do_maths(maths_rules, 9)
# >>> 20
这是一个愚蠢的例子,但你应该明白你要找的是什么

委托是定义方法签名的类型。实例化委托时,可以将其实例与具有兼容签名的任何方法相关联。您可以调用 (或通过委托实例调用)方法

您在C#中的示例,使用:

intaddone(intx){返回x+1;}
int double_it(int x){返回x*2;}
var math_rules=新列表{add_one,double_it};
int do_数学(IEnumerable数学规则,int x)
{
foreach(数学规则中的var func)
{
x=func(x);
}
返回x;
}
Console.WriteLine(做数学(数学规则,9));
//打印“20”

是的,您可以使用委托。为此,请使用
Func
。 比如:

总的来说:

Func<int, int> myFunc = new Func<int, int>(addone);
myFunc(5); // to use it, you can pass it as you like
Func myFunc=newfunc(addone);
myFunc(5);//要使用它,你可以随意传递
代码示例:

static int add_one(int x)
{
    return x + 1;
}
static int double_it(int x)
{
    return x * 2;
}
static int do_maths(List<Func<int, int>> math_rules, int x)
{
    foreach(var func in math_rules)
        x = func(x);
    return x;
}
static void Main(string[] Args)
{
    List<Func<int, int>> math_rules = new List<Func<int, int>>();
    math_rules.Add(new Func<int, int>(add_one));
    math_rules.Add(new Func<int, int>(double_it));
    Console.WriteLine(do_maths(math_rules, 9)); // 20
}
static int add_one(int x)
{
返回x+1;
}
静态整数双_it(整数x)
{
返回x*2;
}
静态int do_数学(列出数学规则,int x)
{
foreach(数学规则中的var func)
x=func(x);
返回x;
}
静态void Main(字符串[]参数)
{
列出数学规则=新建列表();
数学规则.Add(新函数(Add_one));
添加(新的Func(double_it));
Console.WriteLine(做数学(数学规则,9));//20
}
或者按照评论中的建议使用lambdas:

static int do_maths(List<Func<int, int>> math_rules, int x)
{
    foreach(var func in math_rules)
        x = func(x);
    return x;
}
static void Main(string[] Args)
{
    List<Func<int, int>> math_rules = new List<Func<int, int>>();
    math_rules.Add(new Func<int, int>((x) => (x + 1)));
    math_rules.Add(new Func<int, int>((x) => (x * 2)));
    Console.WriteLine(do_maths(math_rules, 9)); // 20
}
static int do_数学(列出数学规则,int x)
{
foreach(数学规则中的var func)
x=func(x);
返回x;
}
静态void Main(字符串[]参数)
{
列出数学规则=新建列表();
添加(新函数((x)=>(x+1));
添加(新函数((x)=>(x*2));
Console.WriteLine(做数学(数学规则,9));//20
}

或使用lambda
Func add_one=(x)=>x+1我应该提到我在.NET3.5上。不确定这对下面的解决方案是否重要。
static int add_one(int x)
{
    return x + 1;
}
static int double_it(int x)
{
    return x * 2;
}
static int do_maths(List<Func<int, int>> math_rules, int x)
{
    foreach(var func in math_rules)
        x = func(x);
    return x;
}
static void Main(string[] Args)
{
    List<Func<int, int>> math_rules = new List<Func<int, int>>();
    math_rules.Add(new Func<int, int>(add_one));
    math_rules.Add(new Func<int, int>(double_it));
    Console.WriteLine(do_maths(math_rules, 9)); // 20
}
static int do_maths(List<Func<int, int>> math_rules, int x)
{
    foreach(var func in math_rules)
        x = func(x);
    return x;
}
static void Main(string[] Args)
{
    List<Func<int, int>> math_rules = new List<Func<int, int>>();
    math_rules.Add(new Func<int, int>((x) => (x + 1)));
    math_rules.Add(new Func<int, int>((x) => (x * 2)));
    Console.WriteLine(do_maths(math_rules, 9)); // 20
}