简体   繁体   中英

What is the Technical Term for C# inline variable assignment and evaluation?

I was reading through some code on Github recently and i came across the following line,

if (((tmp = rx.IndexOf("<")) >= 0) && (rx.IndexOf(">") > tmp ))

specifically the

      (tmp = rx.IndexOf("<") >= 0) 

And the immediate use of the tmp variable in a comparison in the same line in the next part of the if statement

(rx.IndexOf(">") > tmp )

in which a variable is being set by a string,indexOf() method, and then the 'assignment statement itself' is being evaluated with a greater-or-equal-to equality operator.

At first i thought this was a typo, but on evaluating the code via a simple console app, i found it to be valid, and a great shortcut.

The question is "What is the technical term for this?" as i could find no explanation in various C# help sites.

An example console app to demonstrate how the statement was used.

public static void Main()
    {
        // first test - the actual code I found in gitHub 
        int tmp;
        int tmp2;
        string rx = "  < test>";

        // the below line is the subject of the question.


        if (((tmp = rx.IndexOf("<")) >= 0) && (rx.IndexOf(">") > tmp )){ 

            Console.WriteLine("The Close brace is after the opening brace!");
        }

        // additional test 
        int r;
        Console.WriteLine(r = 25 + 3);
        Console.WriteLine(r);


        // and another 
        int w = -1;
        Console.WriteLine(" The index of '<' is greater than 0 : " + _
         ((w = rx.IndexOf("<")) > 0).ToString() + _ 
         " and the value of w is " + w.ToString()); 
    }

The output of the above code is below. Again, I understand the code works, I would like to know what is this called technically?

The Close brace is after the opening brace!
28
28
The index of '<' is greater than 0 : True and the value of w is 2

This is just a consequence of assignment being an expression. It is defined in the C# specification:

7.17.1 Simple Assignment

The = operator is called the simple assignment operator.

The result of a simple assignment expression is the value assigned to the left operand. The result has the same type as the left operand and is always classified as a value.

So the value of

tmp = rx.IndexOf("<")

is the value assigned to tmp which is rx.IndexOf("<") . This value is then compared to 0 in the outer expression.

There is no "technical term". It's an assignment, and a assignment can act as expression or a statement. It's a statement that has a value; Or an Expression where the result value can be ignored. (Unlike x+y, the Result is not allowed to be ignored) It's the same as the prefix and postfix operators i++; It will look less like "inline" in a line like x = y = z;

However, it's not often used, cause it's less readable, as you just proofed. And in your case, assigning a value and using the value in the same expression tree highly depends on evaluation order, which is well defined, but who knows it by heart?

This style of writing will safe you a line of code (by making one line longer) but it will never save you any operation, hence not cause any performance.

So Read it, Understand it, but better don't use it frequent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM