0

Is there a simpler or alternate way to code the following C# sentence?

if (iValue >= 4 && iValue <= 10) {... other code ...}

Do I need to repeat the iValue twice?

I have seen a structure similar to _ = ...some code... but I am not familiar with that leading underscore?

Ravi Makwana
  • 2,782
  • 1
  • 29
  • 41
John D
  • 517
  • 7
  • 22

1 Answers1

5

With C#9 you can use:

if(iValue is >= 4 and <= 10)
{
    Console.WriteLine("in range");
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I was wondering about the performance (micro-optimizations, I know) but this gives literally the same IL as `if(iValue >= 4 && iValue <= 10)`. So that's cool! – CompuChip Oct 26 '22 at 12:08