如何调用Func<;int,int,int>;C#中方法中的参数?
我正在尝试创建一个接受不同方法(如何调用Func<;int,int,int>;C#中方法中的参数?,c#,func,actionmethod,C#,Func,Actionmethod,我正在尝试创建一个接受不同方法(Funcs)作为参数的方法。 我在定义Funcs参数时遇到了一个小问题。 假设我需要调用这样的东西: public static void SomeTestMethod(int number,string str) { Check(MethodOne(number,str)); } 我这里有一个: public static int Check(Func<int,string,int> method) { // some c
Func
s)作为参数的方法。我在定义
Func
s参数时遇到了一个小问题。
假设我需要调用这样的东西:
public static void SomeTestMethod(int number,string str)
{
Check(MethodOne(number,str));
}
我这里有一个:
public static int Check(Func<int,string,int> method)
{
// some conditions
method(where should i get the arguments ?);
}
如果可能,我需要提供此签名:
Check(MethodOne(number,str));
您缺少返回值的类型(最后一个泛型参数表示返回值的类型)。如果您不想Func
返回任何内容,只需使用Action
:
public static void Check(
Action<int, string> method,
int arg1,
string arg2)
{
method(arg1, arg2);
}
公共静态无效检查(
行动方法,
int arg1,
字符串arg2)
{
方法(arg1,arg2);
}
您缺少返回值的类型(最后一个泛型参数表示返回值的类型)。如果您不想Func
返回任何内容,只需使用Action
:
public static void Check(
Action<int, string> method,
int arg1,
string arg2)
{
method(arg1, arg2);
}
公共静态无效检查(
行动方法,
int arg1,
字符串arg2)
{
方法(arg1,arg2);
}
您缺少返回值的类型(最后一个泛型参数表示返回值的类型)。如果您不想Func
返回任何内容,只需使用Action
:
public static void Check(
Action<int, string> method,
int arg1,
string arg2)
{
method(arg1, arg2);
}
公共静态无效检查(
行动方法,
int arg1,
字符串arg2)
{
方法(arg1,arg2);
}
您缺少返回值的类型(最后一个泛型参数表示返回值的类型)。如果您不想Func
返回任何内容,只需使用Action
:
public static void Check(
Action<int, string> method,
int arg1,
string arg2)
{
method(arg1, arg2);
}
公共静态无效检查(
行动方法,
int arg1,
字符串arg2)
{
方法(arg1,arg2);
}
我想你想要这个:
public static void SomeTestMethod(int number,string str)
{
Check( () => MethodOne(number,str));
}
public static int Check(Func<int> method)
{
// some conditions
return method();
}
publicstaticvoidsometestmethod(int-number,stringstr)
{
检查(()=>MethodOne(编号,str));
}
公共静态整型检查(Func方法)
{
//一些条件
返回方法();
}
我想你想要这个:
public static void SomeTestMethod(int number,string str)
{
Check( () => MethodOne(number,str));
}
public static int Check(Func<int> method)
{
// some conditions
return method();
}
publicstaticvoidsometestmethod(int-number,stringstr)
{
检查(()=>MethodOne(编号,str));
}
公共静态整型检查(Func方法)
{
//一些条件
返回方法();
}
我想你想要这个:
public static void SomeTestMethod(int number,string str)
{
Check( () => MethodOne(number,str));
}
public static int Check(Func<int> method)
{
// some conditions
return method();
}
publicstaticvoidsometestmethod(int-number,stringstr)
{
检查(()=>MethodOne(编号,str));
}
公共静态整型检查(Func方法)
{
//一些条件
返回方法();
}
我想你想要这个:
public static void SomeTestMethod(int number,string str)
{
Check( () => MethodOne(number,str));
}
public static int Check(Func<int> method)
{
// some conditions
return method();
}
publicstaticvoidsometestmethod(int-number,stringstr)
{
检查(()=>MethodOne(编号,str));
}
公共静态整型检查(Func方法)
{
//一些条件
返回方法();
}
不清楚您在问什么。如果编译成功,则MethodOne
返回Func
。如果你能提供一个简短但完整的程序来演示这个问题,并解释你想要实现的目标,那将非常有帮助。我们应该如何知道呢?请注意,Func
只接受一个参数(一个int)并返回一个字符串。您可以像这样调用它var result=method(someInt)代码>-大概是检查
然后应该对结果
进行推理,还是返回它?我编辑了这个问题@JonSkeet@StuartLC当前位置错过了,更正了问题。不清楚你在问什么。如果编译成功,则MethodOne
返回Func
。如果你能提供一个简短但完整的程序来演示这个问题,并解释你想要实现的目标,那将非常有帮助。我们应该如何知道呢?请注意,Func
只接受一个参数(一个int)并返回一个字符串。您可以像这样调用它var result=method(someInt)代码>-大概是检查
然后应该对结果
进行推理,还是返回它?我编辑了这个问题@JonSkeet@StuartLC当前位置错过了,更正了问题。不清楚你在问什么。如果编译成功,则MethodOne
返回Func
。如果你能提供一个简短但完整的程序来演示这个问题,并解释你想要实现的目标,那将非常有帮助。我们应该如何知道呢?请注意,Func
只接受一个参数(一个int)并返回一个字符串。您可以像这样调用它var result=method(someInt)代码>-大概是检查
然后应该对结果
进行推理,还是返回它?我编辑了这个问题@JonSkeet@StuartLC当前位置错过了,更正了问题。不清楚你在问什么。如果编译成功,则MethodOne
返回Func
。如果你能提供一个简短但完整的程序来演示这个问题,并解释你想要实现的目标,那将非常有帮助。我们应该如何知道呢?请注意,Func
只接受一个参数(一个int)并返回一个字符串。您可以像这样调用它var result=method(someInt)代码>-大概是检查
然后应该对结果
进行推理,还是返回它?我编辑了这个问题@JonSkeet@StuartLC:错过了,更正了问题。谢谢,我完全忘记了lambdas:),谢谢,我完全忘记了lambdas:),谢谢,我完全忘记了lambdas:),谢谢,我完全忘了兰博达斯:),谢谢