-1

Possible Duplicate:
Solution for overloaded operator constraint in .NET generics
Implementing arithmetic in generics?

I wrote Generics class,but i am having issue as described in title.

class Program
    {
        static void Main(string[] args)
        {
            int a = 1;
            int b = 2;
            int c = 3;

            dynamic obj = new Gen<int>();
            obj.TestLine1(ref a, ref b);
            obj = new Gen<string>();
            obj.TestLine2(ref a, ref b, ref c);
            System.Console.WriteLine(a + " " + b);
            Console.ReadLine();
        }
    }

public class Gen<T>
    {
        public void TestLine1(ref T a, ref T b)
        {
            T temp;
            temp = a;
            a = b;
            b = temp;
        }
        public void TestLine2(ref T a, ref T b, ref T c)
        {
            T temp;
            temp = a;
            a = a + b;
            b = a + c;
            c = a + b;
        }
    }

Inside at method TestLine2(ref T a, ref T b, ref T c) I am getting below issue:

Operator '+' cannot be applied to operands of type 'T' and 'T'
Community
  • 1
  • 1
intelliWork
  • 175
  • 2
  • 4
  • 10
  • 3
    It is a quite common problem in C#. You can't define a constraint on _T_ to specify "_T_ must implement the + operator" – Cédric Bignon Feb 02 '13 at 19:20
  • You have not bounded `T` to be constrained to `+`able types, so the compiler has no way to know that `+` can be applied to `T`. – Matt Ball Feb 02 '13 at 19:21
  • 5
    Duplicate of about 10,000 earlier questions: http://stackoverflow.com/search?q=%5Bc%23%5D+%5Bgenerics%5D+arithmetic Look for an answer written by Marc Gravell, he's solved this nicely – Ben Voigt Feb 02 '13 at 19:21

4 Answers4

8

Since T can be any type, there is no guarantee that T will have a static + operator. In C# there's no way to constrain T to support static operators like +, so you'll have to pass the function to use to combine values of T to TestLine2:

public void TestLine2(ref T a, ref T b, ref T c, Func<T, T, T> op)
{
    T temp;
    temp = a;
    a = op(a, b);
    b = op(a, c);
    c = op(a, b);
}
Lee
  • 142,018
  • 20
  • 234
  • 287
1

You don't know whether T implements the + operator. What if you pass object as the type parameter?

antonijn
  • 5,702
  • 2
  • 26
  • 33
0

Because the type of T is not known until instantiation, there is no guarantee that the type T will support the + operator.

Brett Wolfington
  • 6,587
  • 4
  • 32
  • 51
0

Suppose I created an instance of your class like this: var gen = new Gen<Object>(). Now T means Object everywhere inside this instance of your class. When you call TestLine2(), the method is going to try to add to Objects, something that can't be done in C#.

More broadly, since C# doesn't know ahead of time what type arguments you're going to create a Gen object with, it restricts you to only using methods defined for all objects.

It looks to me like you really want TestLine2 to be a method for consisting strings. Why don't you just make Gen a non-generic class and tell it to use Strings everywhere instead?

Kevin
  • 14,655
  • 24
  • 74
  • 124