Three-dimensional operator of C #

Source: Internet
Author: User

The ternary operator "?:" is sometimes called a conditional operator.

For conditional expression b?x:y, the condition B is evaluated first and then judged. If the value of B is true, the value of x is computed and the result of the operation is the value of x, otherwise, y is computed with a value of Y. A conditional expression never computes X, and also calculates Y. The conditional operator is associated to the right, that is, the grouping is computed from left to right. For example, an expression a?b:c?d:e will be executed in A?b: (c?d:e).

?: The second and third operands control the type of the conditional expression. Set X and Y to be the type of the second and third operands, respectively, then:

If x and Y are the same type, then the type is the type of the conditional expression.

Otherwise, if there is an implicit conversion from X to Y, but there is no conversion of Y to X, then Y is the type of the conditional expression.

Otherwise, if there is an implicit conversion from Y to X, but there is no conversion of x to Y, then X is the type of the conditional expression.

Otherwise, no expression type is defined, and a compile-time error occurs.

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.