Check Number range using case when statement - CSharp Language Basics

CSharp examples for Language Basics:switch

Description

Check Number range using case when statement

Demo Code

using System;/*from   w  w w . j a  va  2 s  . c  om*/
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
class NumericBounds
{
   static void Main()
   {
      CheckBounds(10);
      CheckBounds(10000);
      CheckBounds(10000L);
      CheckBounds(100000L);
      CheckBounds(-10000L);
   }
   static void CheckBounds(object input)
   {
      switch (input)
      {
         case int x when x > 1000:
         case long y when y > 10000L:
            Console.WriteLine("Value is too large");
            break;
         case int x when x < -1000:
         case long y when y < -10000L:
            Console.WriteLine("Value is too low");
            break;
         default:
            Console.WriteLine("Value is in range");
            break;
      }
   }
}

Result


Related Tutorials