ekad gave the correct answer here, but since this is an opportunity for learning:
if
statements expect a bool
value inside their parentheses, which is why you can have things like:
if (true)
and
if (false)
but
if (1)
doesn't make any sense. When you do a comparison (==
) like:
if (a == b)
a
is compared to b
for equivalence, and the statement will evaluate to either true
or false
. This probably makes sense to you already.
Assignment (=
), however, also evaluates to a value, and the value is the value of the left operand. The type returned is the type of the left operand.
batx = 5;
if (x = batx) {
essentially evaluates to
if (5)
Which, as said before, doesn't make sense (in C#, anyway -- this does make sense in C). The reason why I typed all that out is that it explains the compiler error you got.
cannot implicitly convert type 'int' to 'bool'
The compiler expected to find a statement which evaluates to a bool
inside the parentheses. Instead, it found a statement that evaluates to int
.