I was making a generic method and was wondering if there is some way of adding a constraint to a generic type T, such that T has a certain operator, like +, +=, -, -=, etc.
public void TestAdd<T>(T t1, T t2)
{
return t1 + t2;
}
Produces the following error text:
Operator '+' cannot be applied to operands of type 'T' and 'T'
I searched around on Google/SO for a while and couldn't really find anything related.
I think this cannot be done
You can do it less fancy by :
interface IAddable { void Add(object item); }
...
public void TestAdd<T>(T t1, T t2) where T : IAddable
{
return t1.Add(t2);
}
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