1

In c# (nullable types)

int a = 10;
int? b = 20;
int? c = null;

System.Console.WriteLine( a+c??b );

Output is : 20

if (c??b +a) then Output is 30

I don't understand why ..

Prescott
  • 7,312
  • 5
  • 49
  • 70
Vikash
  • 449
  • 1
  • 4
  • 11
  • Others have given the explanation. So this is what you actually want: `a+(c??b)` respectively `(c??b)+a`. And they do agree, because `+` is commutative (even when using nullable integers). – Jeppe Stig Nielsen Sep 17 '12 at 17:11

6 Answers6

2

It's just a matter of precedence.

This:

System.Console.WriteLine(a + c ?? b);

is equivalent to:

System.Console.WriteLine((a + c) ?? b);

a + c is null (because c is null), therefore 20 is printed (the value of b)

Whereas this:

System.Console.WriteLine(c ?? b + a);

is equivalent to:

System.Console.WriteLine(c ?? (b + a));

c is null, therefore the RHS (b + a) is evaluated, and that's 30.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

c??b + a is evaluated as

 c?? (b+a)

or slightly expanded

 c == null ? b + a : c

or if we substitute with the actual values

 null == null ? 20 + 10 : null) 

whereas a +c??b is evaluated as

 (a+c)??b

or if we expand slightly

 (a + c) == null ? b : c+a

or if we substitute with the actual values

 (10 + null) == null ? 20 : 10 + null

which again can be shortened to

 null == null ? 20 : null

the addition operator for nullable types always return null if at least one of the operands are null

Rune FS
  • 21,497
  • 7
  • 62
  • 96
1

When you write:

  System.Console.WriteLine( a+c??b );

It will effectively evaluate as:

        int? temp = a + c;
        System.Console.WriteLine(temp ?? b);

The "temp" above is null, so you get the result of b.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
1

Check here: ECMA-334: 14.2.1 Operator precedence and associativity

The order of precedence of Null Coalescing operator (??) is very less than that of '+'. So, '+' is always evaluated first.

In your case,

int a = 10;
int? b = 20;
int? c = null;

a+c??b evaluates as (a+c)??b = (10+null)??20 = null??20 = 20

c??b+a evaluates as c??(b+a) = null??(10+20) = (10+20) = 30

prashanth
  • 2,059
  • 12
  • 13
  • A good answer. But it's better to write the last line as: `c??b+a` evaluates as `c??(b+a) = null??(10+20) = (10+20) = 30`. If `c` had not been null, the addition would never had been performed. But that's something different than precedence which you explain absolutely correctly. – Jeppe Stig Nielsen Sep 17 '12 at 17:19
  • 1
    worth reading: http://stackoverflow.com/questions/6238074/how-the-right-associative-of-null-coalescing-operator-behaves – prashanth Sep 17 '12 at 18:42
0

Since c is null then the answer is b! That's what your double ? do.

Erre Efe
  • 15,387
  • 10
  • 45
  • 77
0

The ?? operator is used to check for nulls. It returns either the value in question, or a fallback if it is null.

c ?? b means "c, unless c is null, in which case b".

c ?? b + a checks c, and sees that it is null, and uses b (20) instead. It then adds a (10) to get 30.*

a + c ?? b seems to be applying the ?? to a + c rather than just to c, resulting in the whole thing being replaced with b (20).

*Not sure about order of operations in this case, it might be using (b + a) due to c being null.

yoozer8
  • 7,361
  • 7
  • 58
  • 93