\how to call a C# function passed as a parameter c# code example

Example 1: Pass Method as Parameter using C#

public class Class1
{
    public int Method1(string input)
    {
        //... do something
        return 0;
    }

    public int Method2(string input)
    {
        //... do something different
        return 1;
    }

    public bool RunTheMethod(Func<string, int> myMethodName)
    {
        //... do stuff
        int i = myMethodName("My String");
        //... do more stuff
        return true;
    }

    public bool Test()
    {
        return RunTheMethod(Method1);
    }
}

Example 2: c# pass method as parameter

public int Method1(string input)
    {
        return 0;
    }
	
    public bool RunTheMethod(Func<string, int> myMethodName)
    {
        int i = myMethodName("My String");
        return true;
    }

    public bool Test()
    {
        return RunTheMethod(Method1);
    }