Assume a C# program with two nullable decimal properties A and B. The following addition returns the value of A only: var result = A ?? 0 + B ?? 0; The correct usage is: var result = (A ?? 0) + (B ?? 0);
Sample console program:
class Program
{
static void Main(string[] args)
{
A = (decimal)0.11;
B = (decimal)0.69;
NullableDecimalAddition();
Console.ReadLine();
}
public static decimal? A { get; set; }
public static decimal? B { get; set; }
private static void NullableDecimalAddition()
{
decimal result1 = A ?? 0 + B ?? 0;
decimal result2 = (A ?? 0) + (B ?? 0);
Console.WriteLine("result1: " + result1); // = 0.11
Console.WriteLine("result2: " + result2); // = 0.80
}
}
My question is what happens during the calculation with result1. How is the precedence of the + and ?? operators?
tinstaafl's answer is correct. To expand upon it:
The ??
operator is right associative and the +
operator is higher precedence. Therefore A ?? 0 + B ?? 0
is equivalent to A ?? ((0 + B) ?? 0)
, not (A ?? 0) + (B ?? 0)
.
Therefore the action of A ?? 0 + B ?? 0
is:
A
, which you state is of type nullable decimal. If it is non-null, obtain its decimal value as the result. We are now done; B
is never computed.This is not your intended action; you can obtain your intended action with (A ?? 0) + (B ?? 0)
. If you intended by contrast to mean "use zero if either side is null" then you could use (A + B) ?? 0
. The parentheses are unnecessary in the latter but a good idea given that you were confused about the precedence of +
vs ??
.
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