Puzzle 86: harmful brackets
Can you name such a valid C # expression as an invalid expression by adding parentheses to a subexpression, the brackets are added only to indicate the order of values when no brackets are added?
86: harmful brackets
Insert a pair of parentheses to annotate the existing assignment order Program The legality seems to have no effect. In fact, in most cases, there is no impact. However, in some cases, inserting a pair of parentheses that appear to be unaffected may make legal C # programs invalid. This strange situation is caused by the asymmetry of the binary complement of values, as discussed in puzzles 33 and 64.
You may think that the absolute value of the negative number of the minimum long type is 1 greater than the positive number of the maximum long type. C # negative decimal literal constants are not supported. Negative constants of the int and long types are composed of negative operators (-) before the decimal literal constants. This composition is determined by a special language rule: When a decimal literal constant with a value of 9223372036854775808 (263) (without a suffix, or with a suffix L or L) A constant that appears after a flag is followed by a negative unary operator mark (section 7.6.2). The result is a constant of the long type with a value of-9223372036854775808 (-263. In all other cases, such a decimal literal constant belongs to the ulong type [C # Language Specification 2.4.4.2].
Once you know the rule, this puzzle is easy. Character-9223372036854775808 constitutes a valid C # expression. By adding a pair of parentheses to annotate (very unimportant) The assignment order, that is, writing-(9223372036854775808), this rule will be broken. Believe it or not, the following program will surely see a compilation error: Error cs0023: The operator "-" cannot be applied to the operands of the "ulong" type [C # Language Specification 7.6.2], if parentheses are removed, the error will be cleared:
Public class extends onparen
{
Static void main ()
{
System. Console. writeline (-(9223372036854775808 ));
}
}
There is nothing to learn about this puzzle. It is just a rare case, both pure and simple. But you must admit that it is interesting.
C # directory