c# – Is it possible to make a switch depending on the sign of the number?

Question:

Is it possible to do:

switch (Dec1-Dec2)
{
  case <0:  ...
  case 0:   ...
  case >0:  ...
}

?

Answer:

switch (Math.Sign(Dec1 - Dec2))
{
  case -1:  
    ...
    break;
  case  0:
    ...
    break;
  case  1:
    ...
    break;
}

https://docs.microsoft.com/en-us/dotnet/api/system.math.sign?view=netframework-4.8

Scroll to Top