C# Addition between objects that are numbers

  • A+

I'm trying to find the best way to do following:

I have two Objects, object A and B. At some point in the program I know A and B are of type int, double or float. I would like to make an addition to them so A + B = C. C will be typed as we're used to while making addition between ints, floats, and doubles.

For example, if A was int and B float. Then C would be float.

The closest you can come is:

dynamic a = ...; dynamic b = ...; dynamic c = a + b; 

That will perform the appropriate kind of addition, but you won't know the type of the result until execution time.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: