Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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#?

like image 868
Michael Haddad Avatar asked Jun 09 '16 09:06

Michael Haddad


People also ask

What does <> mean in C#?

It is a Generic Type Parameter. A generic type parameter allows you to specify an arbitrary type T to a method at compile-time, without specifying a concrete type in the method or class declaration.

How do you write greater than less than in C#?

x == 5 would return true if x had a value of 5. The 'greater than' and 'less than' operators are used to check if values are greater than or less than another value. For example, x > 5 (if the value of x was 3 than it would return false).


2 Answers

Nope. But you can remove brackets:

bool b = 0 < x && x < 5

or play with math:

bool b = x*x < 5*x
like image 181
omikad Avatar answered Sep 21 '22 20:09

omikad


Latest C# Allow this pattern:

    var counter = Random.Shared.Next(0,5);
    if (counter is > 0 and < 5)
    {
      //Do logic
    }
like image 33
JDeVil Avatar answered Sep 23 '22 20:09

JDeVil