When declaring a binary operator, at least one of the operand types must be the containing type. This sounds a good design decision in general. However, I didn't expect the following code to cause this error:
public class Exp<T>
{
public static Exp<int> operator +(Exp<int> first, Exp<int> second)
{
return null;
}
}
What is the problem with this operator? Why this case falls into operator overloading restrictions of c#? is it dangerous to allow this kind of declaration?
Because the containing type is Exp<T>
, not Exp<int>
. What you are trying to do here is specialization a la C++, which is not possible in C#.
You are in a class of type Exp<T>
, and neither of the parameters in the operator are Exp<T>
, they're both Exp<int>
.
Read this article for the suggested way around this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With