为了账号安全,请及时绑定邮箱和手机立即绑定

如何将一个方法设置为另一个方法的参数

如何将一个方法设置为另一个方法的参数

C#
喵喔喔 2023-07-09 10:20:16
我需要将一个方法设置到另一个函数的参数中。所以它应该看起来像这样(只是伪代码):void toDoAnotherMethod(anotherMethod()) {     anotherMethod();}void justMethod() {     *some stuff to do*}void Start() {    toDoAnotherMethod(justMethod);}但我完全不明白如何在实际代码中做到这一点。有人可以帮助我吗?
查看完整描述

1 回答

?
红糖糍粑

TA贡献1815条经验 获得超6个赞

用途Action:


void toDoAnotherMethod(Action anotherMethod)

{

    anotherMethod();

}


void justMethod()

{

//            *some stuff to do *

}


void Start()

{

    toDoAnotherMethod(justMethod);

}

另外,如果你的方法有参数,你可以使用Action< T1, …>,如果它们返回一个值,你应该使用Func<TResult>, Func<T1, TResult>etc 。


查看完整回答
反对 回复 2023-07-09
  • 1 回答
  • 0 关注
  • 69 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信