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

3

8

Is there a shorthand for this:

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

Something like:

bool b = 0 < x < 5;

In C#?

Softball answered 9/6, 2016 at 9:18 Comment(7)
Nope; you'll just have to do bool b = 0 < x && x < 5;Tallman
Would writing an extension method help?Summersault
@Summersault Using an extension method is going to add more than 4 characters at the call site (and adding " && " only adds 4 characters).Tallman
Its the shothand actually. One more thing you can have in C# Enumerable.Range(1,4).Contains(x)Gaillardia
@Gaillardia I wouldn't recommend an O(N) solution to an O(1) problem...Tallman
@Shaharyar, x can be floatAlisha
Agree with the flaws in this. I just meant to provide another way to achieve it. Its not the best way that's why I commented rather than the answer.Gaillardia
H
6

Latest C# Allow this pattern:

    var counter = Random.Shared.Next(0,5);
    if (counter is > 0 and < 5)
    {
      //Do logic
    }
Haematothermal answered 20/6, 2022 at 9:54 Comment(0)
A
9

Nope. But you can remove brackets:

bool b = 0 < x && x < 5

or play with math:

bool b = x*x < 5*x
Alisha answered 9/6, 2016 at 9:23 Comment(9)
Plus one: I love the second one. But, it might overflow the type (or wrap around for integral types which would be a disaster), so we both know that the first one is the only real feasible choice.Athwart
Sure, I wrote it just for fun. I always use first approachAlisha
@Bathsheba. I deleted my comment (because I had mistook the second option for a serious suggestion), but what I said was brackets make it clearer. That's true, I know how operator precedence works and brackets still just make it read better on the page.Refined
Thanks. I use the brackets for readability. Also, if I use an inequality, I need to actually solve it to understand what it means, which is not very readable. Thanks. :)Softball
@NathanCooper: Personally I find excess parentheses obfuscating and annoying. But I work on scientific libraries so am prepared to be part of the exception group as opposed to the normal group.Athwart
I agree to put excessive brackets in complicated situations, but that expression looks pretty short and standard. Not sure how is it possible to misread itAlisha
Arguing against myself here, note that 0 < x & x < 5 would behave very differently due to & and && being very distant in the precedence table!Athwart
What kind of nanny state is C# trying to enforce? It's valid in C and C++.Athwart
Actually, it will works same as &&. According to MSDN: "Binary & operators are predefined for the integral types and bool. For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true."Alisha
H
6

Latest C# Allow this pattern:

    var counter = Random.Shared.Next(0,5);
    if (counter is > 0 and < 5)
    {
      //Do logic
    }
Haematothermal answered 20/6, 2022 at 9:54 Comment(0)
S
-1

A LINQ solution, not shorter but more readable:

bool b = new[] { 1, 2, 3, 4 }.Contains(x);
Splendor answered 9/6, 2016 at 9:25 Comment(3)
That works for the example in OP's post, but the question is about a general shorthand notation for "Smaller than and greater than". How would you handle bool b = (x > 212) && (x < 45872);?Tragedian
@LarsKristensen - then you can use something like Enumerable.Range(212+1, 45872-212-1)Splendor
Yes, but then it's not a shorthand version.Tragedian

© 2022 - 2024 — McMap. All rights reserved.