0
object bread(food foo)
{
    return foo.ingredient ?? "cheese";
}

If foo exits, but ingredient is null, I get "cheese". My question, including an assumption:
If foo itself is null will "chesse" be returned or will it throw an ArgutmentNullException?

My GUESS is that the NullCoalescingOperator is implemented more or less like this:

object nco(object lhs, object rhs)
{
    if(lhs != null)
        return lhs;
    else
        return rhs;
}

Therefore passing foo.ingredient already causes the exception(since you cannot check a field in an object that you don't have) and thus it is thrown.
Would make sense.

Is this idea correct / how is the nco implemented and why?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Mark
  • 419
  • 1
  • 6
  • 21
  • 1
    Do you mean `??` or `?`? What C# version? I have read about a syntax like `foo?.ingredient` but am not sure this made it into C# yet. – CodeCaster Jul 09 '14 at 11:15
  • 2
    If `foo` is null, you will get `NullReferenceException` –  Jul 09 '14 at 11:15
  • You can assert your own assumption by trying your bread example in a simple test project. What is the reason you didn't do that? – MarkO Jul 09 '14 at 11:15
  • @MarkO quite practically the reason is, that I'm currently on the train, when I started to wonder about this. – Mark Jul 09 '14 at 11:18
  • 2
    Note that in [some versions of C#](https://roslyn.codeplex.com/wikipage?title=Language%20Feature%20Status&referringTitle=Documentation) it will be allowed to use `return foo?.ingredient ?? "cheese";` where the operator `?.` is a new one in C#. See my link. – Jeppe Stig Nielsen Jul 09 '14 at 11:21
  • @Jeppe thanks, "null propagation" is the term I couldn't think of. See for example this (badly-titled) question: [Possible pitfalls of using this (extension method based) shorthand](http://stackoverflow.com/questions/123088/possible-pitfalls-of-using-this-extension-method-based-shorthand). – CodeCaster Jul 09 '14 at 11:23
  • @JeppeStigNielsen cutting edge knowledge right there. That'll be an interesting option in the future. – Mark Jul 09 '14 at 11:25

3 Answers3

5

You will get a NullReferenceException if foo is null.

So you've got to treat this case, with a ternary (if else) rather than a coalesce.

return (foo == null || foo.ingredient == null) 
           ? "cheese" 
           : foo.ingredient;
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
3

Yes, you idea is absolutely correct because:

foo.ingredient ?? "cheese";

Equals to:

foo.ingredient != null ? foo.ingredient : "cheese";

What you might like, in VS2014 CTP they already have new operator ?., which will do what you need:

foo?.ingredient ?? "cheese";
Uriil
  • 11,948
  • 11
  • 47
  • 68
1

Of course it'll throw an exception. You cannot access a property of the object, that's not there. If you want secure yourself from an error, you could write:

return (foo == null) ? "cheese" : (foo.ingredient ?? "cheese");
Tarec
  • 3,268
  • 4
  • 30
  • 47