Click here to Skip to main content
15,905,776 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi,

I am trying to use a generic method within a generic class.But am getting an error as

Operator '+' cannot be applied to operands of type 'T' and 'T'

My code is as -

C#
public class Gen<T>
{
  public T Plus<T>(T par1, T par2)
    {
        return par1 + par2;
    }
}


Any ideas, folks ??
Regards
Rajeev
Posted
Updated 18-Mar-11 16:31pm
v7
Comments
Philippe Mori 31-May-16 18:42pm    
Generics in C# are quite limited (compared to say C++ templates) and thus it is not possible to do that in a simple way.

This is a big problem. This is very wanted behavior but completely impossible with .NET.
Why this thing will not compile. Because the operations '+', '-', etc. are not defined for the generic parameter type. Even if you define this operation, you need to add the constraint to the generic type declaration using the key word "where". It can work, but! The constraint could not be a primitive type!

This is one of two or three biggest architectural problems which haunt .NET in general. Why it is so? Because, despite of common type system (boxing/unboxing) .NET failed to introduce inheritance is primitive type (which is not something mystical or impossible; so is done, for example, in Ada). In .NET you cannot even write C++ typedef (which is not a type definition as well).

There is a known work-around. The generic class for numeric calculation can operate not with primitive types, but with its wrapper classes, and the class or interface can be a constraint in the generic classes. But let's see how much it costs!

To develop such constraints, you will need to develop a class including all arithmetic operations plus all System.Math operations. And how about operation with mixed-type operands? And what for? You will do such time-consuming thing once with single, and once with double, and what else? It's a long boring work which won't really pay off (but will somewhat decrease performance). Giving up in favor of, say, using double type only would be much more practical.

Unfortunately…

—SA
 
Share this answer
 
v3
Comments
Albin Abel 19-Mar-11 0:15am    
Good thoughts. my 100+
Sergey Alexandrovich Kryukov 19-Mar-11 0:23am    
Thank you very much, Albin. It just so happens that I have to correct you in one more Answer today... see the last one about "active current windows"...
--SA
rajivpande86 19-Mar-11 11:09am    
Thanks SA for clarifying on that..I had googled already but couldn't find a proper reason for what was blocking my way..Didn't know that I had reached the core of the very few architectural problems with Asp.Net.. :)
Sergey Alexandrovich Kryukov 19-Mar-11 15:07pm    
You did, and not only you... :-)
You're welcome.
Are you going to formally accept my Answer?
--SA
Philippe Mori 31-May-16 20:41pm    
Well, while it cannot be done in C#, one could use templates in C++/CLI to avoid repeating same code and then have public overload for each types that would call the implementation template (since templates member functions do not seems to be exported for ref class).
So in some cases it might be an alternative...
Hi if you want to add a type of class together then you need to define in that classes how to add them. See the example below.

C#
public class t{
     public int Value;
     public static t operator +(t obj1,t obj2)
     {
          obj1.Value +=  obj2.Value;
          return obj1;
     }
 }
 public class Gen<T>
 {
     public t Plus/*removed <T>*/(t par1, t par2) //SA!!! 2011-18-03: a fix to make code compile
     {
         return par1 + par2;
     }
 }



Now you can use this like..


XML
Gen<t> test = new Gen<t>();
t test1 = new t();
test1.Value = 40;
t test2 = new t();
test2.Value = 50;
t test3 = test.Plus<t>(test1, test2);
MessageBox.Show(test3.Value.ToString());


As per the example you have shown in the question.
 
Share this answer
 
v4
Comments
Espen Harlinn 17-Mar-11 19:30pm    
Nice and instructive, my 5
Albin Abel 18-Mar-11 0:32am    
Thanks Espen Harlinn
Albin Abel 18-Mar-11 22:42pm    
Thanks SAKryukov
Sergey Alexandrovich Kryukov 18-Mar-11 22:43pm    
Albin, first, I had to fix your code to compile. But after the fix, the class is not really generic. It would be generic if you replace t with T. This is impossible. See my comment to John's Answer.
--SA
Sergey Alexandrovich Kryukov 18-Mar-11 22:57pm    
Work-around is possible, but hardly practical. I put my answer, please see.
I did not vote this time, because the problem is difficult...
--SA
No guarantees, but try overloading the + operator in your class

C#
public static Gen<T> operator +(Gen<T> a, Gen<T> b)
{
    return (Gen<T>)(a.Value + b.Value);
}


EDIT (31 May 2016) -------------------------------------------------------

For those that missed it, I prefaced my answer with "no guarantees". To assist the perception-impaired, I bolded and italicized the qualifying text so it's easier to see.

Voting my answer with a one is bullsh|t (and this is one of the reasons I no longer participate in Q/A).
 
Share this answer
 
v4
Comments
rajivpande86 17-Mar-11 13:52pm    
Hi John,I do not want to pass a generic object as a parameter.I want to just pass a variable of type 'T'.Am not so sure but I guess what I am looking for is not far away.. :)
#realJSOP 17-Mar-11 14:36pm    
My solution is still viable. You just have to change the location of the ovverloaded operator (see Aldin's solution to see what I mean).
Albin Abel 17-Mar-11 22:55pm    
Yes. OP didn't understand the general form what you have been told. Looks like they need ready made code to copy paste. I follow your answer and made that example, not to copy paste anyway. Thanks and my 5
rajivpande86 19-Mar-11 10:52am    
Albin,I just want to let you know that I was not looking for ready-made code.I was just working on Generics and got stuck here.I didn't want code;rather I wanted to know why my code was not working..
Albin Abel 19-Mar-11 13:05pm    
Sorry for my words. The answer for your question why it is not working is already answered here. It is because the generic class doesn't know how to add the two objects as the generic you have shown has no constraints. So any object can't be combine just like a number right. But what we are trying to do is getting you a solution. Later we found it is a complicated one. So in order to achieve it better take an alternate way. Cost effective!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900