Which one should I use? decimal.Add () or "+"

I have a question that I can not find the answer. Imagine that I have two decimal numbers. When do I want to sum the numbers that one and why should I use?

option1:

var num1 = 10.456m; var num2 = 12.033m; var result = decimal.Add(num1, num2); 

option2:

 var num1 = 10.456m; var num2 = 12.033m; var result = num1 + num2; 
+8
c #
source share
3 answers

They are exactly the same. The Decimal class overloaded the + operator, calling the same method. Therefore, you should use one that seems more readable to you. Personally, I prefer the second approach.

Operator

+ (kindly provided by the reflector):

 [SecuritySafeCritical, __DynamicallyInvokable] public static decimal operator +(decimal d1, decimal d2) { FCallAddSub(ref d1, ref d2, 0); return d1; } 

and Add method:

 [SecuritySafeCritical, __DynamicallyInvokable] public static decimal Add(decimal d1, decimal d2) { FCallAddSub(ref d1, ref d2, 0); return d1; } 

Strict equivalence in terms of IL and performance.

+11
source share

Both C # give you several ways to solve the problems that you choose the one that is more convenient for you. I personally use num1 + num2.

0
source share

They are both exactly the same, only different in appearance.

0
source share

All Articles