0

I want to pass a function as a parameter to another function (with actual values) and later execute it. Here is JavaScript code:

function functionToPass1 (x,y) {
    console.log(x, y);
}

function functionToPass2 (x,y,z) {
    console.log(x, y, z);
}

function mainFunction(param1, param2, functionToPass){
  console.log(param1, param2);
  functionToPass();

}

mainFunction("a", "b", function(){
    functionToPass2(1, 2, 3) ;
});

How to write this in C# (or VB.Net)?

2

3 Answers 3

5

Func<> Delegates are what you are looking for to pass methods in another method. I wrote a small example below, you can apply it according to your needs. Func<> is a generic delegate. It can be Func<int,string> Func<int,int,bool> and so on. The last one represents the return type and the others represent the input parameter types of method.

static void Main(string[] args)
{
    MethodB(MethodA);
    Console.ReadLine();
}

static string MethodA(string message) //Func<string,string>
{
    return message;
}

static void MethodB(Func<string, string> method)
{  
    Console.WriteLine(method("I am MethodA"));
}

For more detailed information, you can check this link => https://www.tutorialsteacher.com/csharp/csharp-func-delegate

Sign up to request clarification or add additional context in comments.

7 Comments

Now I need to set that string message in the first call in Main, something like methodb(methoda ("message")) is that possible?
You can write directly method("I am MethodA");, no need for method.Invoke("I am MethodA"); (the .Invoke() part, I mean)
You can use lambda like this MethodB(x => x = "I am a new method"); but this is not MethodA. This is just another method
method?.Invoke(...) is preferred in case method is null.
Sometimes it's difficult to emphasize what one is trying to do, I want to pass both function and arguments to another and invoke it there, in all examples here and there only function is passed, for now I think that JavaScript is capable of such thing
|
1

Thank you all for contributing, your help meant all to me. The only thing I needed to figure out that is an anonymous function to be passed. C# equivalent is like this:

string functionToPass1(int i, string x, string y)
{
  return string.Join(',', new[] { i.ToString(), x, y });
}

string functionToPass2(int i, string x, string y, string z)
{
  return string.Join(',', new[] { i.ToString(), x, y, z });
}

string mainFunction(string param1, string param2, Func<int,string>? functionToPass)
{
  Console.WriteLine(param1);
  Console.WriteLine(param2);
  var res = "";
  int i = 5;
  if (functionToPass != null)
    res = functionToPass(i);
  return res;
}

var res = "";

res = mainFunction("a", "b", (x) =>
{
  return functionToPass2(x, "1", "2", "3");
});

Console.WriteLine(res);
Console.WriteLine("---------------");

res = mainFunction("a", "b", (x) =>
{
  return functionToPass1(x, "1", "2");
});

Console.WriteLine(res);
Console.WriteLine("---------------");

res = mainFunction("a", "b", null);

Console.WriteLine(res);
Console.WriteLine("---------------");
Console.ReadLine();

Comments

1

I didn't delete the first comment in case it might be useful to other people in the future. I made some changes for the code you refreshed. I tried to create a generic structure as much as possible. However, Javascript is much more flexible than C#. In other words, even if the structure is generic, at some points we have to break this genericity.

        static void Main(string[] args)
        {
            MainFunction<string, string, int>("a", "b", FunctionToPass2<int, int, int>);
            Console.ReadLine();
        }

        static void FunctionToPass1<T1, T2>(T1 x, T2 y) 
        {
            Console.WriteLine(x + " " + y);
        }

        static void FunctionToPass2<T1, T2, T3>(T1 x, T2 y, T3 z)
        {
            Console.WriteLine(x + " " + y + " " + z);
        }

        static void MainFunction<T1, T2, T3>(T1 param1, T2 param2, Action<T3, T3, T3> functionToPass)
        {
            Console.WriteLine(param1 + " " + param2 + "\n");
            FunctionToPass2(1, 2, 3);
        }

If our parameter types are certain, I believe we will come up with a much simpler solution by not writing it generic. For example, I assume that T3 for Action<T3, T3, T3> delegate is int for this solution because your arguments are integer. Here we have to break the 'generic' structure a bit.

3 Comments

I was struggling to get a more generic way, so I can pass functions with different numbers of arguments + I need to change some parameters inside MainFunction and then (x) =>{return functionToPass1(x, "1", "2");} came so handy to me. It is actually SPOT ON! Can I ask you to upwote my question I think it deserves a positive zero at least :)
Of course:) you are right:)
Thanks ABI I appreciate your effort on this, I think that someone may find this thread useful and helpful and that we did good job on it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.