Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.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#_Parameters_Delegates_Anonymous Function - Fatal编程技术网

c#匿名函数作为参数

c#匿名函数作为参数,c#,parameters,delegates,anonymous-function,C#,Parameters,Delegates,Anonymous Function,我现在从c#开始,希望使用匿名函数与字段(二维数组)交互。我想要达到的目标应该是这样的 //...somewhere in class private int[][] field1; interactWithElements(field1, { x++; //...more complex stuff }); private void interactWithElements(int[][] field, Func

我现在从c#开始,希望使用匿名函数与字段(二维数组)交互。我想要达到的目标应该是这样的

//...somewhere in class
private int[][] field1;

interactWithElements(field1, {
  x++;
  //...more complex stuff
});

private void interactWithElements(int[][] field, 
                                  Func anonymusFunction(int x)) {
  for (int x = 0; x < field.Length; x++) {
    for (int y = 0; y < field[0].Length; y++)) {
      anonymusFunction(field[x][y]);
    }
  }
}
//…在课堂上的某个地方
私有int[][]字段1;
与元素交互(字段1{
x++;
//…更复杂的东西
});
私有void interactiveWithElements(int[][]字段,
函数(int x)){
for(int x=0;x
在c#中可能出现类似的情况吗?如果是这样,我该怎么做? 也许是代表们

谢谢你,亲爱的。

你就快到了:

//...somewhere in class
private int[][] field1;

interactWithElements(field1, x => {
  x++;
  //...more complex stuff
});
您在这里定义的是一个lamda表达式
x=>{x++;}
,有关它的信息,请参见此

其余的只需要正确的参数

private void interactWithElements(
    int[][] field, 
    Action<int> anonymusFunction) 
{
  for (int x = 0; x < field.Length; x++) {
    for (int y = 0; y < field[0].Length; y++)) { // <- you may have meant x instead of 0
      anonymusFunction(field[x][y]);
    }
  }
}
private void与元素交互(
int[][]字段,
行动(功能)
{
for(int x=0;x对于(int y=0;yfield[x]。Length
而不是
field[0]。Length
在我的代码中没有任何区别字段总是一个完美的矩形,否则其他代码可能会发现这个问题;)很酷:)