1

I reviewed code and I found this operator & is there any difference with && if I want to change to &&

public bool isTemplate(string template)
{
    return (isValid() & lc.templates.Any(x => x.Id.Equals(template, StringComparison.Current)));
}
sese sese
  • 53
  • 1
  • 4
  • For that particular code, it looks like you should indeed change to use `&&` – Matthew Watson May 12 '21 at 08:54
  • Operator `&&` compare boolean values (it is a test of equality to true (0) or false(1)), if first is false, next is not evaluated and result is false, and so on. Operator `&` "compare" bits (it is a binary calculation in fact), all are evaluated. –  May 12 '21 at 09:04
  • See [Boolean algebra](https://en.wikipedia.org/wiki/Boolean_algebra) and [Conditional (computer programming)](https://en.wikipedia.org/wiki/Conditional_(computer_programming)) –  May 12 '21 at 09:10

1 Answers1

4

From documentation:

& operator always evaluates both operands.

&& operator evaluates the right-hand operand only if it's necessary.

Backs
  • 24,430
  • 5
  • 58
  • 85