c# - "?" type modifer precedence vs logical and operator (&) vs address-of operator (&) -
update: seems not beingness clear plenty of asking (and question developed on time lost track bit), here tl;dr version:
var test1 = byte & b; // compiles var test2 = byte? & b; // not compile var test3 = byte? && b; // compiles this means - understand - ? type modifier has lower precedence (as it's not operator might not best word) & operator, higher && operator. so? described in standard?
and original question:
while trying figure out reply sec puzzle jon skeet's first-class blogpost, a tale of 2 puzzles, faced problem:
unsafe private void test<t>(t a, bool b) { var test1 = byte? & b; // not compile var test2 = byte? && b; // compiles var test3 = byte ? & b : & b; // compiles } here using unsafe context actual goal requires (e.g.: 3rd line), not necessary reproducing issue raise. (however might have effect, introduces address-of operator alternative & symbol.)
the first line not compile (the others do), gives next error message:
syntax error, ':' expected this means in case compiler sees line as
var test1 = (a byte) ? &b [: missing part complains about]; while in sec line sees as:
var test2 = (a byte?) && (b); i checked operator precedence (here), , order (from highest lowest) following: &, &&, ?: , lone not explain why first line not compile while sec (or @ to the lowest degree not me - maybe wrong...) edit: understand why sec compiles, please don't concentrate on in answers.
my next hunch somehow precedence (if there such thing) ? type modifier somewhere between 2 (or in fact three) operators (& , &&). can so? if not please explain exact behavior experiencing? evaluating order of ? type modifier described somewhere in standard?
edit: aware there unary address-of operator (actually that's trick trying utilize solution...), plays role here, question still stays same.
in same unsafe context happily compile:
var test1 = true & b; var test2 = true && b; // or var test1 = byte & b; var test2 = byte && b; so think must related ? modifier/operator, , not solely address-of operator taking precedence (otherwise 2 test1 lines not compile).
p.s.: know can add together parentheses code, compile, want avoid that:
var test = (a byte?) & b; // compiles update: experimented roslyn little, , thought might thought attach asts various statements:
var test1 = byte & b; var test2 = byte? & b; var test3 = byte? && b; i want emphasise not looking solution original problem in linked article (of course of study looking one, inquire not give reply here please, find out on own.) also, please don't comment if on wrong track in finding solution, mentioned puzzle give context specific problem, , provide good-enough excuse writing code this.
the clue here unsafe keyword. there 2 different & operators - bitwise-and & operator you're used to, address-of & operator, not has higher precedence, evaluated right-to-left, unary operators.
this means &b evaluated first, , results in pointer value. rest of statement is, compiler complains, unparseable. it's either (a byte?) (address), or (as compiler tries parse it) (a byte) ? (address) , missing :.
i same compile error when replacing & + or -, both symbols can either unary or binary operators.
the reason sec statement compiles fine there isn't unary, right-to-left high-precedence && operator.
c# operators
No comments:
Post a Comment