Search code examples
c#conditional-statementsbitwise-operatorslogical-operators

Short-circuit logic should be used in boolean contexts, is there any difference using "&", "&&"


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)));
}

Solution

  • From documentation:

    & operator always evaluates both operands.

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