Reputation: 388
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
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