Reprint: The difference between & and && in C #

Source: Internet
Author: User

Original address: http://www.cnblogs.com/chinafine/archive/2009/02/17/1392309.html thank Bo Master to share!

Binary operators (&) pre-defined binary & operators for integer and bool types. For integer,&, the bitwise "and" of the operands are computed. The logical "and" of the operands is computed for the bool operand,&, that is, the result is true if and only if the two operands are true. The condition and operator (&&) performs the logical AND (&) operation of the boolean operand.

The following example best describes the problem:

usingSystem;classTest {Static BOOLfn1 () {Console.WriteLine ("Fn1 called"); return false; }     Static BOOLfn2 () {Console.WriteLine ("Fn2 called"); return true; }      Public Static voidMain () {Console.WriteLine ("Regular and:"); Console.WriteLine ("result is {0}", Fn1 () &fn2 ()); Console.WriteLine ("Short-circuit and:"); Console.WriteLine ("result is {0}", FN1 () &&fn2 ()); } }

Output:

Regular and:
Fn1 called
Fn2 called
Result is False
Short-circuit and:
Fn1 called
Result is False

BTW
(&) can also be used as a unary operator to return the address of an operand

Ps.
(|) Same as the (| |) operator

Reprint: The difference between & and && in C #

Related Article

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.