What does the "&=" in this C# code do?

38,333

Solution 1

It's not a bitwise operator when it's applied to boolean operators.

It's the same as:

someBoolean = someBoolean & someString.ToUpperInvariant().Equals("blah");

You usually see the short-cut and operator &&, but the operator & is also an and operator when applied to booleans, only it doesn't do the short-cut bit.

You can use the && operator instead (but there is no &&= operator) to possibly save on some calculations. If the someBoolean contains false, the second operand will not be evaluated:

someBoolean = someBoolean && someString.ToUpperInvariant().Equals("blah");

In your special case, the variable is set to true on the line before, so the and operation is completely unneccesary. You can just evaluate the expression and assign to the variable. Also, instead of converting the string and then comparing, you should use a comparison that handles the way you want it compared:

bool someBoolean =
  "blah".Equals(someString, StringComparison.InvariantCultureIgnoreCase);

Solution 2

It's the equivalent of += for the & operator.

Solution 3

It is short for:

someBoolean = someBoolean & someString.ToUpperInvariant().Equals("blah");

See MSDN (&= operator).

Solution 4

someBoolean = someBoolean & someString.ToUpperInvariant().Equals("blah");

which, in this case, before someBoolean is true, means

someBoolean = someString.ToUpperInvariant().Equals("blah");

Solution 5

As Guffa pointed, there IS a difference between & and &&. I would not say you can, but rather you SHOULD use && instead of & : & makes your code geeker, but && makes your code more readable... and more performant. The following shows how :

class Program
{
    static void Main(string[] args)
    {
        Stopwatch Chrono = Stopwatch.StartNew();
        if (false & Verifier())
            Console.WriteLine("OK");
        Chrono.Stop();
        Console.WriteLine(Chrono.Elapsed);

        Chrono.Restart();
        if (false && Verifier())
            Console.WriteLine("OK");
        Chrono.Stop();
        Console.WriteLine(Chrono.Elapsed);
    }

    public static bool Verifier()
    {
        // Long test
        Thread.Sleep(2000);
        return true;
    }
}
Share:
38,333
Matthew Steven Monkan
Author by

Matthew Steven Monkan

Veteran software development expert who enjoys mentoring, leading, and solving problems that benefit society.

Updated on March 08, 2020

Comments

  • Matthew Steven Monkan
    Matthew Steven Monkan about 4 years

    I came across some code that looks like this:

    string someString;
    
    ...
    
    bool someBoolean = true;
    someBoolean &= someString.ToUpperInvariant().Equals("blah");
    

    Why would I use the bitwise operator instead of "="?

  • Gobiner
    Gobiner almost 13 years
    Bitwise operators are still bitwise operators when they are applied to System.Booleans. However, since C# sets 'true' equal to 1, they wind up having the same truth table as the boolean operators. However, any non-zero integer can serve as a valid true value. See dotnetpad.net/ViewPaste/6wYCZ3QmNUClKXiYi113Pw for an example.