I'm trying to add several integers together using NULL Coalesce
, in which at least 2 of the integers may be NULL, in that case, assign 0 to such integers and then add.
var total = votes[0].Value ?? 0 + votes[1].Value ?? 0 + votes[2].Value ?? 0 + votes[3].Value ?? 0;
total
returns the value of votes[0].Value
instead of addition of all four variables.
Is there a way I can get the total of all the integers?
That code is equivalent to:
So it should be rather apparent now why it returns
votes[0].Value
rather than the sum of all of the non-null values.