在函数中传递函数作为参数

sub*_*iah 5 c#

如何将参数作为函数传递

drh*_*ris 6

你可能正在寻找代表.

public delegate void MyDelegate(int myInt, string myString);
public void FunctionToCall(int i, string s)
{
    Console.WriteLine(s + " [" + i.ToString() + "]");
}
public void MethodWithFunctionPointer(MyDelegate callback)
{
    callback(5, "The value is: ");
}
Run Code Online (Sandbox Code Playgroud)

然后,称之为:

MethodWithFunctionPointer(FunctionToCall);
Run Code Online (Sandbox Code Playgroud)