3

When this code finishes, what is the result of myObject?

object myObject = "something";
object yourObject = null;

myObject = null ?? yourObject;
skaffman
  • 398,947
  • 96
  • 818
  • 769
Brad
  • 15,361
  • 6
  • 36
  • 57
  • 5
    You could always try it out & see, rather than asking here... What would you _expect_ the result to be? – thecoop Oct 19 '10 at 18:00
  • 3
    I think it's an interesting question, even though the answer is obvious to more experienced developers. At any rate, Stack Overflow exists to answer the difficult as well as the trivial, I think (non-redundant) simple questions should be encouraged. – Anthony Pegram Oct 19 '10 at 18:07
  • @leppie Now, do you mean null as in 'null' or null as 'undefined'? ;-) –  Oct 19 '10 at 18:09
  • possible duplicate of [?? Null Coalescing Operator --> What does coalescing mean?](http://stackoverflow.com/questions/770186/null-coalescing-operator-what-does-coalescing-mean) I voted to close because the answer can be found here and in other related posts. –  Oct 19 '10 at 18:09
  • 1
    @pst, I looked at that post and did not find the answer, buried as it may be, in the definitions of the English term 'coalesce'. – Brad Oct 19 '10 at 18:11

3 Answers3

2

myObject will be null

This gets translated to -

if (null == null)
    myObject = yourObject;
else
    myObject = null;
Ed Guiness
  • 34,602
  • 16
  • 110
  • 145
Sachin Shanbhag
  • 54,530
  • 11
  • 89
  • 103
1

The coalesce operator translates to this:

x ?? y
x != null ? x : y

Therefore what you have:

myObject = null != null ? null : yourObject;

Which is actually pretty pointless since null will always be null.

Dismissile
  • 32,564
  • 38
  • 174
  • 263
  • of course my example was very simplistic and non-realistic. Thank you for your explanation. – Brad Oct 19 '10 at 18:09
  • Yeah I realize that. The point of the coalesce operator is to provide a value to use when something is null...more than likely so you don't get a null reference exception if you try and use the value. An example: someString = (someString ?? "").ToLower(); – Dismissile Oct 19 '10 at 18:15
  • 1
    It is also useful for assigning nullable types to non-nullable types. Example: int? x = 1; int y = x ?? -1; If you tried to assign y to x without using ?? or doing x.Value you would get an error. And if you did x.Value and the value is null then you are also in trouble. – Dismissile Oct 19 '10 at 18:18
1

Just for kicks, here is a small table:

A    ?? B    -> R
---------------------
a    ?? any  -> a; where a is not-null
null ?? b    -> b; for any b
null ?? null -> null; implied from previous

And since ?? is just a (surprise!) right-associated infix operator, x ?? y ?? z --> x ?? (y ?? z). Like && and ||, ?? is also a short-circuiting operation.

...from ?? Operator (C# Reference):

It (??) returns the left-hand operand if it is not null; otherwise it returns the right operand.

...from the C# 3.0 Language reference:

A null coalescing expression of the form a ?? b requires a to be of a nullable type or reference type. If a is non-null, the result of a ?? b is a; otherwise, the result is b. The operation evaluates b only if a is null.