c# – Why is the compiler suggesting this change to me?

Question:

I work with SharpDevelop .

When doing this check:

string aux1 = "123";
string aux2 = "456";

bool iguales = false;

if(aux1.equals(aux2))
    iguales = true;

The compiler suggests I change it to:

string aux1 = "123";
string aux2 = "456";

bool iguales = false;

iguales |= aux1.equals(aux2);

If this particular code is analyzed, it does it well, but it does it well because the Boolean is false , but if it were true this equivalence would not work.

Does this mean that the compiler "knows" that the bool before the comparison is false and that is why it does the conversion, and if it were true it would not do it?

Wouldn't it be easier to directly assign the value of that comparison to the bool as in the following lines?

string aux1 = "123";
string aux2 = "456";

bool iguales = false;

iguales = aux1.equals(aux2);

Answer:

Totally agree with you Although for this case you can make the assumption as I am going to leave it to you below.

string aux1 = "123";
string aux2 = "456";

bool iguales = aux1.equals(aux2);
Scroll to Top