I want to do something in the same spirit of this non-compiling code
public static B operator + (Func<A,B> f, A a) {
return f (a);
}
It there a way to specify the types A
and B
? I've tried
public static B operator +<A,B> (Func<A,B> f, A a) {
return f (a);
}
but its too good to be true.
No, operators can't be generic in C#. You can overload operators within generic types, but the operators themselves can't have type parameters.
If you look at the syntax for user-defined operators in the C# spec (e.g. in section 10.10 of the C# 5 spec) you'll see that it doesn't have anywhere for type parameters (or type parameter constraints) to be added.
See more on this question at Stackoverflow