C# - Error CS0019 - Operator '+' cannot be applied to operands of type 'T' and 'T'

The most likely cause:
You created a generic class or struct, and defined an overload for the plus operator.

Since T is a generic type, the compiler cannot determine whether it supports addition.

T could be an int, a string, an IPAddress, a DataSet, etc. Any object. The most the compiler knows (without using constraints), is that T is a System.Object.

The ValueContainer<T> class below demonstrates the CS0019 error:

  class ValueContainer<T>
      public T Value { get; set; }
      public ValueContainer(T value)
          this.Value = value;
      public static ValueContainer<T> operator+(ValueContainer<T> left, ValueContainer<T> right)
          var result = left.Value + right.Value; // CS0019
          return new ValueContainer<T>(result);

Ads by Google

Ask a question, send a comment, or report a problem - click here to contact me.

© Richard McGrath