C # confusing 86: harmful brackets

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.