Precedence of ?? (null coalescing) operator

  • Thread starter Thread starter Jon Shemitz
  • Start date Start date
J

Jon Shemitz

The new ?? operator doesn't seem to be in the precedence table in
VS.05
<ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_csref/html/214e7b83-1a41-4f7c-9867-64e9c0bab39f.htm>,
and I can't seem to find anything about it online.

What's the precedence of the ?? operator?

[It doesn't seem like it would be "Primary" - up there with x.y, f(x),
a[x], x++, x--, new, typeof, checked, and unchecked - but it doesn't
seem "Unary", either. I hope there's not a new category between Unary
and Arithmetic!]
 
The ECMA Standard for C#
(http://www.ecma-international.org/publications/standards/Ecma-334.htm, pag.
149) lists the null coalescing op ?? near the bottom of the precedence
table, above the ternary (conditional) and the assignment operators.

Many thanks. Though knowing that there are now FIFTEEN levels of
precedence feels decidedly yucky. ;-)
The new ?? operator doesn't seem to be in the precedence table in
VS.05
<ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_csref/html/214e7b83-1a41-4f7c-9867-64e9c0bab39f.htm>,
and I can't seem to find anything about it online.

What's the precedence of the ?? operator?

[It doesn't seem like it would be "Primary" - up there with x.y, f(x),
a[x], x++, x--, new, typeof, checked, and unchecked - but it doesn't
seem "Unary", either. I hope there's not a new category between Unary
and Arithmetic!]
 
Jon,
Many thanks. Though knowing that there are now FIFTEEN levels of
precedence feels decidedly yucky. ;-)

Agreed. But anyway, operator precedence in C# seems to me a lot better
designed than in Pascal, right?

Regards,

Octavio
 
Octavio said:
Agreed. But anyway, operator precedence in C# seems to me a lot better
designed than in Pascal, right?

Actually, Delphi has only four levels of precedence. That involves
silliness like calling bitwise `and` (C#'s & operator) a "multiplier",
but four is better than fifteen, imho.

Though, I do find that C# precedence rarely gets in the way. It
usually does the right thing, without a lot of parens. I'm a bit
dubious about `(Type)(Expr)` code, where you have to parenthesize the
whole expression because casting has such high precedence, but I don't
find that this is all that common an issue.
 
Jon,
Though, I do find that C# precedence rarely gets in the way. It
usually does the right thing, without a lot of parens.

That's exactly what I was thinking about, I find annoying the need for a lot
of parens in Delphi whenever 'and' and 'or' are involved in an expression...

Best regards from a sure buyer of your book,

Octavio Hernandez
Madrid, Spain
 
Octavio said:
Best regards from a sure buyer of your book,

Thanks. It's moving along nicely, after a long pause while my shoulder
ligaments grew back after a bike accident. Doing 2nd draft now -
checking against RTM bits, responding to tech reviewer comments, &c.
 
Octavio Hernandez said:
I find annoying the need for a lot of parens in

I have the exact opposite view. I generally code as if there are only
two levels:
* and / come before + and -
Everything else is determined by the parens.(*)

(*) OK, I don't really follow this literally -- e.g. I won't wrap a
obj.member in parens so that the dot-operator is handled first, but further
down the chart -- where the order gets murky, it often better to add a few
"unnecessary parens" rather then count on both the original programmer & the
maintence programmer agree with the standard on the order.

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
James,
it often better to add a few "unnecessary parens"...

I understand your point of view, too. I guess the fact that generally
maintain my own code obviously has some influence on my opinion.

Regards - Octavio
 
Octavio Hernandez said:
I understand your point of view, too. I guess the fact that generally
maintain my own code obviously has some influence on my opinion.

Out of interest - how often do you need to maintain code you wrote over
a year ago? I usually find that code I wrote but haven't looked at for
over a year is as "foreign" to me as code written by others. Maybe
that's just me though. It does mean I try to make things as readable as
possible though :)
 
Jon Skeet said:
Out of interest - how often do you need to maintain code you wrote over
a year ago?

Not very often. Occasionally one of the little applets I throw
together to manage my website fails when it runs into a new edge
condition.
I usually find that code I wrote but haven't looked at for
over a year is as "foreign" to me as code written by others. Maybe
that's just me though. It does mean I try to make things as readable as
possible though :)

I also have that experience. Same response, too.
 
Jon,
I usually find that code I wrote but haven't looked at for
over a year is as "foreign" to me as code written by others.

LOL! Yes, it surely may look a little bit "foreign", but not as "foreign" as
if it was written by someone else I guess :-)

Regards - Octavio
 
Back
Top