[英]How do you return a reference of a method with Func<>?
I have a simple bit of code but am struggling to work out the correct syntax to return the method as a reference so it can be used. 我有一些简单的代码,但是正在努力找出正确的语法以将该方法作为参考返回,以便可以使用它。
This is what i am trying to do: 这就是我想要做的:
public static Func<float> GetFunction(Functions functions)
{
switch (functions)
{
case Functions.Linear: return // how do i return the func here?
break;
default: return null;
}
}
public static float Linear(float k) // am trying to return this
{
return k;
}
The idea then is i will be able to do something like: 然后的想法是我将能够做类似的事情:
Func<float> func = GetFunction(Functions.Linear);
print func(10);
I am not sure how i create the return though. 我不确定如何创建回报。
For a delegate to float Linear(float k)
, a function that takes a float
argument and has a float
return value, the generic Func<float, float>
delegate would have to be used instead of Func<float>
, which encapsulates a function that takes no argument and returns a float
. 若要使
float Linear(float k)
这个具有float
参数并具有float
返回值的函数,则必须使用泛型Func<float, float>
委托代替封装了一个函数的Func<float>
没有参数并返回float
。 As in: 如:
public static Func<float, float> GetFunction(Functions functions)
{
switch (functions)
{
case Functions.Linear: return Linear;
break;
default: return null;
}
}
public static float Linear(float k) // am trying to return this
{
return k;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.