pass function as parameter c# code example
Example 1: pass function in as variable C#
void Function(Func<int> function)
{
function();
}
void DoStuff()
{
Function(function);
}
int function()
{
Console.WriteLine("Returns 0");
return 0;
}
Example 2: Pass Method as Parameter using C#
public class Class1
{
public int Method1(string input)
{
return 0;
}
public int Method2(string input)
{
return 1;
}
public bool RunTheMethod(Func<string, int> myMethodName)
{
int i = myMethodName("My String");
return true;
}
public bool Test()
{
return RunTheMethod(Method1);
}
}