c# - 在函数中将函数作为参数传递

标签 c#

如何将参数作为函数传递

最佳答案

您可能正在寻找 delegates .

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: ");
}

然后,调用它:

MethodWithFunctionPointer(FunctionToCall);

关于c# - 在函数中将函数作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3115479/

相关文章:

c# - 计算字节数组定时流的帧速率和大小

c# - 在函数参数中捕获异常

c# - 这是多态的例子吗?

c# - 使用字符串变量访问其他脚本中的公共(public)变量

c# - ProcessExit 与 DomainUnload

c# - 在 MVC 之外使用 Razor 模板引擎(具有完整的 mvc razor 支持)

c# - HTTPS 客户端/服务器通信错误 : The underlying connection was closed: An unexpected error occurred on a send

c# - 更改用户昵称 Discord 的命令

c# - 更改进程的基址指针

c# - 在 ASP.NET MVC Core 应用程序 (RC2) 中显示项目版本