I have this method
public static T F<T>(T arg)
{
return arg;
}
and I want to create a Func delegate to F. I try this
public Func<T, T> FuncF = F;
but it's not syntactically correct. How I can do that.
I have this method
public static T F<T>(T arg)
{
return arg;
}
and I want to create a Func delegate to F. I try this
public Func<T, T> FuncF = F;
but it's not syntactically correct. How I can do that.
Only classes and methods can be generic in and of themselves. A field that uses generic parameters must be within a generic class context:
Or if the type parameter for
F
should not be connected toFuncF
, then just use a different name for one of the parameters:You can only (re)expose generics from a generic class, or in a generic method. Otherwise, you will need to provide definite types for
T
(e.g. for a local variable or as a field or property in a non-generic class). Examples:F<int>
is a method which would be assignable to aFunc<int, int>
variable.F<double>
is a different method which would be assignable to aFunc<double, double>
variable.But you cannot have a generic variables. C# just doesn't work like that.
The closest thing to generic variables is a field of a generic class: