2

Possible Duplicates:
How to cast an object into its type?
Solution for overloaded operator constraint in .NET generics

Hi All,

Is it possible to have a generic Addition method? something like:

public void Add<T>(T a, T b)
{
    T c = a + b;//Error
}

Actually operands '+' can not be applied to Type T. Is there any way around?

Community
  • 1
  • 1
Wondering
  • 4,950
  • 22
  • 71
  • 90
  • Duplicate: http://stackoverflow.com/questions/1031821/how-to-cast-an-object-into-its-type/1031834#1031834 – Noldorin Jul 06 '09 at 14:15
  • Is this really a generic scenario? or is there an abstract object(s) that have that (+) addition logic? – BigBlondeViking Jul 06 '09 at 14:17
  • See my answer in that question for a solution for user-defined types. Other answers discuss how it can be done for integral value types. – Noldorin Jul 06 '09 at 14:17
  • Sigh, if that question is an exact duplicate, maybe it should be titled as such, or edited so that it is clear. Without reading the answers I would have NO CLUE that it was asking almost the same thing. No wonder he couldn't find it. – Andrew Mar 21 '12 at 02:19

0 Answers0