What value is verified in a condition operation with value assignment to a variable?

Asked

Viewed 211 times

3

I have a method that does a check and return of this method stored in a type variable bool.

When I perform this operation C# tests the value of the variable or the return value of the method?

Example:

 bool retorno = default(bool);
 if ((retorno = conta.Saca(Convert.ToDouble(txtValor.Text))))
 {          
      // regra de négocio
 }

2 answers

6


You have to look at the table of precedence and associativity of operators.

The parentheses used already give a guarantee that everything inside it will be executed before and the result of this whole expression is that it will be used by if. Therefore

Within this expression all that is on the right side of the = will be executed first since the association is right to left. Anyway it would also take time to execute the assignment since it has low precedence.

Picking up the expression conta.Saca(Convert.ToDouble(txtValor.Text)) will first execute the . since he has the highest precedence along with parentheses. He goes first because the associativity is left to right, and he’s more left. The same goes for what is within each parenthesis. Then the execution will be:

temp = txtValor.Text
temp = Convert.ToDouble(temp)
temp = conta.Saca(temp)
retorno = conta.Saca(temp)
if (retorno)

using System;

public class C {
    public static int Main() {
        var conta = new Conta();
        var txtValor = new Form();
        bool retorno;
        if ((retorno = conta.Saca(Convert.ToDouble(txtValor.Text)))) {
            Console.WriteLine(retorno);
            return 1;
        }
        return 0;
     }
}

public class Conta {
    public bool Saca(double x) => true;
}

public class Form {
    public String Text;
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

In fact the IL shows this:

IL_000b: ldloc.0              //bool retorno;
IL_000c: ldfld string Form::Text //txtValor.Text
IL_0011: call float64 [mscorlib]System.Convert::ToDouble(string) //Convert.ToDouble(txtValor.Text)
IL_0016: callvirt instance bool Conta::Saca(float64) //conta.Saca(Convert.ToDouble(txtValor.Text))))
IL_001b: dup                  // Duplicate the value on the top of the stack
IL_001c: stloc.1              //retorno = conta.Saca(Convert.ToDouble(txtValor.Text)))
IL_001d: brfalse.s IL_0027    //if ((retorno = conta.Saca(Convert.ToDouble(txtValor.Text)))) {

Behold complete in Sharplab.

Precedence table:

Category Opradores Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right

4

The Code will test the value of the return variable, which in the case received the return value of the method

bool retorno = default(bool);
if ((retorno = conta.Saca(Convert.ToDouble(txtValor.Text))))

is equivalent to

bool retorno = default(bool);
retorno = conta.Saca(Convert.ToDouble(txtValor.Text));
if (retorno)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.