Search code examples
c#expressionshorthand

Is there a shorthand for smaller than AND greater than, in C#?


Is there a shorthand for this:

bool b = (x > 0) && (x < 5);

Something like:

bool b = 0 < x < 5;

In C#?


Solution

  • Latest C# Allow this pattern:

        var counter = Random.Shared.Next(0,5);
        if (counter is > 0 and < 5)
        {
          //Do logic
        }