user10216769
user10216769

Reputation: 33

operator & cannot be applied to operands of type bool and byte in C#

I am trying to use string and byte in the same condition but getting an error

operator & cannot be applied to operands of type bool and byte

        string m = "";
        string mi= "";
        byte Active = 0;
        
        if ((m== mi) & Active)
        {

        }

Upvotes: 0

Views: 359

Answers (1)

Bercovici Adrian
Bercovici Adrian

Reputation: 9360

The unary operator & can be performed over a boolean. In your case the left operand is a boolean , while the right operand needs to be an expression that returns also a boolean:

if( (m==mi) & expression_that_returns_boolean ){
}

System.byte is a keyword that is used to declare a variable which can store an unsigned value range from 0 to 255.

Thus your if statement effectively becomes :

if (boolean & byte )

operator & cannot be applied to operands of type bool and byte

Therefore the right operand of your & operator should be a expression whose result is a boolean

In your case you should create an expression that applied over your byte returns a boolean:

byte Active=0;
if( (m==mi) & expression(Active) )

The expression could be : Active==[some_integer]

Resulting: if( (m==mi) & Active==0 )

Upvotes: 2

Related Questions