The difference between & and && in C #

Source: Internet
Author: User

Siki asked us to check the difference between & and && so ...

1) & and && can be used as the logical AND operator, representing the logic and (and), when the result of an expression on either side of the operator is true, the entire result is true, otherwise, the result is false if one of the parties is false.

2) && also has a short-circuit function, that is, if the first expression is false, the second expression is no longer evaluated, for example, for an if (str! = null &&!str.equals (")) expression, when STR is NULL, The subsequent expression will not be executed, so nullpointerexception will not appear, and if && is changed to &, then it will continue to determine whether equals, and actually STR is NULL, The NullPointerException exception is thrown. In the example below, if (x==33 & ++y>0) y grows, if (x==33 && ++y>0) does not grow

            int x = n;            int y = 2;            if (x >= && ++y > 0)            {                Console.WriteLine (y);            }            Console.WriteLine (y);            if (x >= & y++ > 0)            {                Console.WriteLine (y);            }            Console.WriteLine (y);            Console.readkey ();

3) & can also be used as a bitwise operator, when an expression on either side of the & operator is not a Boolean,,& represents bitwise AND operation, and we typically use 0x0f to perform a & operation with an integer to get the minimum 4 bit bits of that integer, for example, 0x31 & the result of 0x0f is 0x01.

The difference between & and && in C #

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.