Is there a shorthand for this:
bool b = (x > 0) && (x < 5);
Something like:
bool b = 0 < x < 5;
In C#?
Is there a shorthand for this:
bool b = (x > 0) && (x < 5);
Something like:
bool b = 0 < x < 5;
In C#?
Latest C# Allow this pattern:
var counter = Random.Shared.Next(0,5);
if (counter is > 0 and < 5)
{
//Do logic
}
Nope. But you can remove brackets:
bool b = 0 < x && x < 5
or play with math:
bool b = x*x < 5*x
0 < x & x < 5
would behave very differently due to &
and &&
being very distant in the precedence table! –
Athwart Latest C# Allow this pattern:
var counter = Random.Shared.Next(0,5);
if (counter is > 0 and < 5)
{
//Do logic
}
A LINQ solution, not shorter but more readable:
bool b = new[] { 1, 2, 3, 4 }.Contains(x);
bool b = (x > 212) && (x < 45872);
? –
Tragedian Enumerable.Range(212+1, 45872-212-1)
–
Splendor © 2022 - 2024 — McMap. All rights reserved.
bool b = 0 < x && x < 5;
– Tallmanshothand
actually. One more thing you can have in C#Enumerable.Range(1,4).Contains(x)
– Gaillardiaflaws
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