Search code examples
c#integercomparisonequality-operator

Short method to check if integer is 0 and return a boolean


Can this be done in a better way?

public bool AreMoreNeeded() => EntitiesNeededCount == 0 ? true : false;

I am using this to check if an integer is 0 and return a boolean based on the operation result .


Solution

  • yes, remove the true and false:

    public bool AreMoreNeeded() => EntitiesNeededCount == 0;
    

    the equality operator :

    The equality operator == returns true if its operands are equal, false otherwise.

    EDIT:

    You can make it even a little more shorter and make it a property instead of a method:

    public bool AreMoreNeeded => EntitiesNeededCount == 0;