如何将委托作为参数传递

时间:2013-07-18 04:03:12

标签: c# parameters delegates void

我想传入一个void或一个int / string / bool(返回一个值)动态地这样。

Delay(MyVoid);//I wont to execute a delay here, after the delay it will execute the the param/void like so...
public static void MyVoid()
{
    MessageBox.Show("The void has started!");
}
public async Task MyAsyncMethod(void V)
{
    await Task.Delay(2000);
    V()
}

ps,我尝试过使用Delegates,但不能将它作为参数使用。

1 个答案:

答案 0 :(得分:4)

使用Action委托执行返回void:

的方法
public async Task MyAsyncMethod(Action V)
{
    await Task.Delay(2000);
    V();
}

Func<T>表示返回某个值的方法

public async Task MyAsyncMethod(Func<int> V)
{
    await Task.Delay(2000);
    int result = V();
}
相关问题