Groups | Blog | Home
all groups > c# > december 2005 >

c# : Precedence of ?? (null coalescing) operator


Jon Shemitz
12/31/2005 8:11:00 PM
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!]

--
Octavio Hernandez
1/1/2006 1:11:33 PM
Jon,

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.

Regards,

Octavio


"Jon Shemitz" <jon@midnightbeach.com> escribió en el mensaje
news:43B75654.AE98F03A@midnightbeach.com...
[quoted text, click to view]

Jon Shemitz
1/1/2006 3:32:51 PM
[quoted text, click to view]

Many thanks. Though knowing that there are now FIFTEEN levels of
precedence feels decidedly yucky. ;-)

[quoted text, click to view]

--
Jon Shemitz
1/2/2006 3:19:56 PM
[quoted text, click to view]

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 Shemitz
1/2/2006 9:20:28 PM
[quoted text, click to view]

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
1/2/2006 10:57:35 PM
Jon,

[quoted text, click to view]

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

Regards,

Octavio

Octavio Hernandez
1/3/2006 1:03:23 AM
Jon,

[quoted text, click to view]

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



Jon Shemitz
1/3/2006 1:20:52 PM
[quoted text, click to view]

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.

[quoted text, click to view]

I also have that experience. Same response, too.

--
James Curran
1/3/2006 1:25:06 PM
[quoted text, click to view]

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

Jon Skeet [C# MVP]
1/3/2006 9:01:08 PM
[quoted text, click to view]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Octavio Hernandez
1/3/2006 9:46:36 PM
James,

[quoted text, click to view]

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
1/3/2006 10:55:27 PM
Jon,

[quoted text, click to view]

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



AddThis Social Bookmark Button