Hleb Butkevich
Hleb Butkevich

Reputation: 388

How to set a method to the parameter of another method

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.

Upvotes: 0

Views: 183

Answers (1)

user3188639
user3188639

Reputation:

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.

Upvotes: 4

Related Questions