pass function with parameter c# code example
Example 1: 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);
}
}
Example 2: pass function in as variable C#
void Function(Func<int> function)
{
function();
}
void DoStuff()
{
Function(function);
}
int function()
{
Console.WriteLine("Returns 0");
return 0;
}