My understanding of C # expressions

Source: Internet
Author: User

This morning I saw "do you really know a ^ = (B ^ = (a ^ = B)?" Article,ArticleAn interesting expression: A ^ = (B ^ = (a ^ = B );Program: 

1 Int A =   2 ;
2 Int B =   9 ;
3 A ^ = (B ^ = ( ^ = B ));
4 Console. writeline ( " {0}, {1} " , A. tostring (), B. tostring ());

The final calculation result is not expected 9, 2, but 0, 2. There is also one expression: 

1 Int I = 0 ;
2 Int J = (I ++ ) + (I ++ );
3 J =?

What is the J value? 2? No. The correct result is 1.

Why is there such a result? Is it a compiler bug? In fact, the compiler may not be a mathematical idiot, but our understanding of the computer is incorrect.

In C #, expression calculation follows a rule:Calculated from left to right.

For example, Z = x operation Y. The computer calculates the values of x and y in sequence, then operationx and Y, and finally assigns the values to Z.

Similarly, if z = x operation1 (x operation2 y), the computer regards (x operation2 y) as a variable temp. In this case, Z = x operation1 temp. Then, the computer calculates the temp expression, presses the stack, computes the temp expression, and outputs the result. This is a recursive process. Note: In this process, X and temp are two parallel expressions. If X is reoperated in temp, the result of X is not affected. Therefore, the expression rule is calculated from left to right, and X is left. The calculation is complete. If it is Z = temp operation1 X, the operation on X in temp will affect X itself.

Okay. After theoretical analysis, let's take a look at why the results of the above two expressions are surprising.

For the first expression: A ^ = (B ^ = (a ^ = B )). First, the operator is decomposed into a = a ^ (B = B ^ (A = a ^ B), which makes it clearer.

1. The computer decomposition expression is a = a ^ temp1. Calculate a on the left. A is a variable, so a is replaced by 2. Next, we calculated the temp1 expression on the right and found that it was complicated and could not be directly replaced by a value. Therefore, we started to calculate the temp1 stack.

2. temp1 is B = B ^ (A = a ^ B ). On the left side of the calculation, B is a variable and can be directly assigned to 9. The expression on the right is replaced by temp2.

3. temp2 is a = a ^ B. On the left side of the calculation, A is a variable, and 2 is replaced; B is also a variable, and 9 is replaced. And calculate temp2 = 2 ^ 9 = 11.

4. regression calculation Temp 1 = 9 ^ temp2 = 9 ^ 11 = 2, that is, B = 2.

5. regression calculation A = 2 ^ temp1 = 2 ^ 2 = 0. That is, a = 0.

OK. The result of 0 and 2 is displayed.

For the second expression: J = (I ++) + (I ++ );.

1. Calculate the expression on the left, I ++. Because it is a suffix, the (I ++) expression returns I. At this time, I = 0, meaning 0 is returned.

2. Execute the ++ operator. 0 ++ is 1, So I = 1;

3. Calculate the expression (I ++) On the right, which is also a suffix. Therefore, I is returned. At this time, I = 1, which means 1 is returned;

4. Execute the ++ operator. 1 ++ is 2, so I = 2;

5 is assigned to J. J = 0 + 1 = 1.

In summary, it is not a compiler bug, but a computer's practice will always be a dead-end.

And,In actual projects, do not use such expressions. Instead, write a few more lines and do not pile them in one line. We must follow 《CodeSpecifications.This is also to improve the readability and maintainability of the Code.

Finally, I will give you a question:

Int I = 0; Int J = (I --) + (I ++) + (++ I); what are the calculation results of I and J?

 

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.