Is there a way to named parameters of a generic Func<...> to have support of intellisense?
Example:
Func<int, int, int> f1 = new Func<int, int, int>(
(a, b) => { return a b }
);
f1(2, 3);
CodePudding user response:
No. You can define your own delegate/interface, though.
public delegate int DoTheThing(int a, int b);
or
public interface IThingDoer {
int DoTheThing(int a, int b);
}
CodePudding user response:
You can't do this with Func<...>
since it is already has names for the type parameters (it's just a normal generic type).
You could define a delegate instead (which has to be defined at class level - you can't define it within a method or as a method parameter):
public delegate int MyFunc(int a, int b);
Then you just use it like:
public static int MyMethod(MyFunc myfunc)
{
return myfunc(1, 2);
}
public static int MyOtherMethod()
{
return MyMethod((a, b) => a b);
}
One somewhat grungy workaround could be to use a tuple for the arguments:
public static int MyMethod(Func<(int a, int b), int> myFunc)
{
return myFunc((1, 2));
}
public static int MyOtherMethod()
{
return MyMethod(tuple => tuple.a tuple.b);
}
Then intellisense will tell you the names of the tuple members. I wouldn't do this though - I'm just mentioning it for completeness.