0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.