[英]c# - how to pass function as parameter (with parameters as values) without invoking it
I want to pass a function as a parameter to another function (with actual values) and later execute it.我想将一个 function 作为参数传递给另一个 function(带有实际值),然后执行它。 Here is JavaScript code:
这是 JavaScript 代码:
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)?如何在 C#(或 VB.Net)中写这个?
Func<> Delegates are what you are looking for to pass methods in another method. Func<> 委托是您正在寻找以另一种方法传递方法的方法。 I wrote a small example below, you can apply it according to your needs.
下面我写了一个小例子,大家可以根据自己的需要来应用。 Func<> is a generic delegate.
Func<> 是一个通用委托。 It can be Func<int,string> Func<int,int,bool> and so on.
它可以是 Func<int,string> Func<int,int,bool> 等等。 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有关更多详细信息,您可以查看此链接 => https://www.tutorialsteacher.com/csharp/csharp-func-delegate
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.
我唯一需要弄清楚的是要传递的匿名 function 。 C# equivalent is like this:
C# 等效如下:
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();
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#.
但是,Javascript 比 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.
例如,我假设 Action<T3, T3, T3> 委托的 T3 对于此解决方案是 int,因为您的 arguments 是 integer。 Here we have to break the 'generic' structure a bit.
在这里,我们必须稍微打破“通用”结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.