Please Help me to solve this problem
The below code is not working. An error occurred during compilation. The error message as follows.
Operator '+' cannot be applied to operands of type 'T' and 'T'
//My Code Here....
namespace GenericClass{
class Program {
static void
Gen
genint.Add(5, 6);
Gen
genfloat.Add(3.4F, 3.5F);
}
class Gen
{
public void Add(T a1, T a2)
{
T c1=a1+a2;
Console.WriteLine(c1.ToString());
}
}
}
}
Krushna ChopadePosted Jul 24, 2017, 12:25 AM
AlanPosted Oct 6, 2007, 3:38 PM
The problem is that T can be absolutely anything and so the compiler can't be sure that it will support the '+' operator. Worse still, there's no constraint that can be applied which would limit T to types which support or overload the '+' operator.
I did make a suggestion on Microsft Connect a while ago that 'operator constraints' should be introduced into .NET generics and received reasonable support for it. I was told that they were considering it for a future release. However, they won't be in C# 3.0 so don't hold your breath!