I have a question that I can not find the answer. Imagine that I have two decimal numbers. When I want to sum the numbers which 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;
They are absolutely the same. The Decimal
class has overloaded the +
operator calling the same method. So you should use the one that you feel more readable. Personally I prefer the second approach.
+
operator (courtesy of reflector):
[SecuritySafeCritical, __DynamicallyInvokable]
public static decimal operator +(decimal d1, decimal d2)
{
FCallAddSub(ref d1, ref d2, 0);
return d1;
}
and the 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With