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)?
CodePudding user response:
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
CodePudding user response:
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();
CodePudding user response:
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.