Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8178275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:43:25+00:00 2026-06-06T23:43:25+00:00

In C#, the switch statement doesn’t allow cases to span ranges of values. I

  • 0

In C#, the switch statement doesn’t allow cases to span ranges of values. I don’t like the idea of using if-else loops for this purpose, so are there any other ways to check numeric ranges in C#?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:43:26+00:00Added an answer on June 6, 2026 at 11:43 pm

    You can use a HashTable respectively Dictionary to create a mapping of Condition => Action.

    Example:

    class Programm
    {
        static void Main()
        {
            var myNum = 12;
    
            var cases = new Dictionary<Func<int, bool>, Action>
            { 
                { x => x < 3 ,    () => Console.WriteLine("Smaller than 3")   } ,
                { x => x < 30 ,   () => Console.WriteLine("Smaller than 30")  } ,
                { x => x < 300 ,  () => Console.WriteLine("Smaller than 300") } 
            };
    
            cases.First(kvp => kvp.Key(myNum)).Value();
        }
    }
    

    This technique is a general alternative to switch, especially if the actions consists only of one line (like a method call).

    And if you’re a fan of type aliases:

    using Int32Condition = System.Collections.Generic.Dictionary<System.Func<System.Int32, System.Boolean>, System.Action>;
    ...
        var cases = new Int32Condition()
        { 
            { x => x < 3 ,    () => Console.WriteLine("Smaller than 3")   } ,
            { x => x < 30 ,   () => Console.WriteLine("Smaller than 30")  } ,
            { x => x < 300 ,  () => Console.WriteLine("Smaller than 300") } 
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a switch statement with 13 cases, each case only has an
I have a switch case statement that doesn't work. I've checked the input, it's
I am using a switch-statement to switch between modes on a the jqPlot jQuery
Can someone show me why this switch statement doesn't output the same as the
my switch statement has about ten outcome, but some of them need 1/2 loops
I'm trying to group time values in MS Access. I have a switch statement,
I'm trying to implement a switch case statement like this: switch (active.attr('class')){ case video:
I have a switch statement and I must be overlooking something, because I don't
I am using a switch statement on a $_GET variable to produce the result
Does anyone know if it's possible to include a range in a switch statement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.