-1

Generics are used to decouple logic from data type.

public class Calc<T>
{
   public T Add(T a, T b)
   {
       return (a+b);
   }
}

But this is throwing the below compile time error

Operator + cannot be applied on type T.

I am not understanding why so. because if it allows from main.cs

main()
{
     Calc<int> obj = new Calc<int>();
     int c = obj.Add(10,20);
}

Can somebody please explain why I am getting Build errors??

Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • @ManfredRadlwimmer, I know what is Generics – Kgn-web Feb 14 '17 at 08:58
  • 3
    Apparently not. If you don't add any restrictions on your type it is simply a `object`. Objects don't have a `+` operator. Generics in C# are not exactly the same as in other languages. – Manfred Radlwimmer Feb 14 '17 at 08:59
  • @ManfredRadlwimmer How should I do my operation.If I pass float it should return float, if int return int – Kgn-web Feb 14 '17 at 09:01
  • See [Constrain type to allow addition/subtraction operations (+/-) in C#](http://stackoverflow.com/questions/5516459/constrain-type-to-allow-addition-subtraction-operations-in-c-sharp) – Georg Patscheider Feb 14 '17 at 09:02
  • Additional info about why you can't put operator constraints on generic types can be found [here](https://stackoverflow.com/questions/5997107/is-there-a-generic-constraint-i-could-use-for-the-operator) and [here](https://stackoverflow.com/questions/147646/solution-for-overloaded-operator-constraint-in-net-generics) – Manfred Radlwimmer Feb 14 '17 at 09:03
  • When it comes to primitive types (int, bool, string, double, etc.) your only option is to check the type and cast the parameters. – Manfred Radlwimmer Feb 14 '17 at 09:04
  • @ManfredRadlwimmer. Thanks for the link – Kgn-web Feb 14 '17 at 09:04

4 Answers4

2

C# generics don't support arbitrary operators. The exact (possibly virtual) method must be known at compile-time of the generic type. Since the generic type argument in your example isn't constrained at all, you can only use the members of the object type, which doesn't include a + operator.

There's no way to use C# generics to do what you're trying to do, sorry. The best you can do is a bunch of type-checks for a few known types (and the appropriate casts, which are tricky with value types), or using reflection (dynamic in particular will work great).

Luaan
  • 62,244
  • 7
  • 97
  • 116
1

Unrestricted generics like your Calc<T> must be able to compile with any type applied, not all types support the + operand so your code does not compile. This is regardless of what specific types you create your calling code with.

You can restrict the type of T and thus gain access to more methods by doing Calc<T> where T:object or Calc<T> where T: IComparable which would allow you to:

public T CompareTo(T a, T b)
{
   return (a.CompareTo(b));
}

Since all T must now implement IComparable. Unfortunately Int32 does not implement any interface which defines the + operator or any addition method. So there is no way to implement that statement you are trying.

David Waters
  • 11,979
  • 7
  • 41
  • 76
0

C# generics are different from C++ templates. The C# code cannot be compiled if generic type doesn't have definition for used method (operator+ in your case), while C++ compiler applies this method to the actual template argument type.

Ilya Ivanov
  • 123
  • 6
0

Addition is not allowed because there is no guarantee that passed types have operator + overload.
There is a way to accomplish that but it will work as long as you pass types that have operator +. Otherwise, RuntimeBinderException will be thrown. Also, there is some impact on performance, but unless code is performance critical, shouldn't be a problem.

public T Add<T>(T a, T b)
{
  dynamic da = a, db = b;
  return da + db;
}
Shadowed
  • 956
  • 7
  • 19