0

I need to set a method into the parameter of another function. So it should look something like this (just pseudocode):

void toDoAnotherMethod(anotherMethod()) {
     anotherMethod();
}

void justMethod() {
     *some stuff to do*
}

void Start() {
    toDoAnotherMethod(justMethod);
}

But I totally don't understand how to do this thing in real code. Can somebody help me?

Thanks.

  • Possible duplicate of [Passing a function as a function parameter](https://stackoverflow.com/questions/54772578/passing-a-function-as-a-function-parameter) – derHugo Jul 22 '19 at 14:25

1 Answers1

4

Use Action:

void toDoAnotherMethod(Action anotherMethod)
{
    anotherMethod();
}

void justMethod()
{
//            *some stuff to do *
}

void Start()
{
    toDoAnotherMethod(justMethod);
}

Also, if your methods have parameters, you can use Action< T1, …>, and if they return a value, you should use Func<TResult>, Func<T1, TResult> etc.