将带有参数的方法作为参数传递的快速方法?

fre*_*oat 3 c# parameters methods

让我先介绍一下我对 C# 非常熟悉的事实。话虽如此,我正在寻找一种方法来传递带有参数的方法作为参数。 理想情况下,我想做的是:

static void Main(string[] args)
{
    methodQueue ( methodOne( x, y ));
}

static void methodOne (var x, var y)
{
    //...do stuff
}

static void methodQueue (method parameter)
{
    //...wait
    //...execute the parameter statement
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以指出我正确的方向吗?

Mar*_*oth 5

这应该做你想做的。它实际上是将无参数方法传递给您的函数,但 delegate(){methodOne( 1, 2 );} 正在创建一个匿名函数,该函数使用适当的参数调用 methodOne。

我想在输入之前测试它,但只有 .net framework 2.0 因此我的方法。

public delegate void QueuedMethod();

static void Main(string[] args)
{
    methodQueue(delegate(){methodOne( 1, 2 );});
    methodQueue(delegate(){methodTwo( 3, 4 );});
}

static void methodOne (int x, int y)
{

}

static void methodQueue (QueuedMethod parameter)
{
    parameter(); //run the method
    //...wait
    //...execute the parameter statement
}
Run Code Online (Sandbox Code Playgroud)