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

  • Home
  • SEARCH
  • 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 898299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:01:19+00:00 2026-05-15T15:01:19+00:00

How can I do this elegantly with C#? For example, a number can be

  • 0

How can I do this elegantly with C#?

For example, a number can be between 1 and 100.

I know a simple if (x >= 1 && x <= 100) would suffice; but with a lot of syntax sugar and new features constantly added to C#/.Net this question is about more idiomatic (one can all it elegance) ways to write that.

Performance is not a concern, but please add performance note to solutions that are not O(1) as people may copy-paste the suggestions.

  • 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-05-15T15:01:20+00:00Added an answer on May 15, 2026 at 3:01 pm

    There are a lot of options:

    int x = 30;
    if (Enumerable.Range(1,100).Contains(x))  //true
    

    And indeed basic if more elegantly can be written with reversing order in the first check:

    if (1 <= x && x <= 100)   //true
    

    Also, check out this SO post for regex options.

    Notes:

    • LINQ solution is strictly for style points – since Contains iterates over all items its complexity is O(range_size) and not O(1) normally expected from a range check.
      More generic version for other ranges (notice that second argument is count, not end):

      if (Enumerable.Range(start, end - start + 1).Contains(x)
      
    • There is temptation to write if solution without && like 1 <= x <= 100 – that look really elegant, but in C# leads to a syntax error "Operator ‘<=’ cannot be applied to operands of type ‘bool’ and ‘int’"

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I guess the solution for this is quite simple, but I've been thinking about
How do I implement error handling elegantly? For example, my data access layer can
OK, I think this will be fairly simple, but my numpy-fu is not quite
I would like to know if there is a non-artificial example, where mutual recursion
How can this be done more elegant and without creating new objects? List<Move> playerMoves
Can this MS-Access query be improved? i.e. the SELECT TOP 1 col FROM Table2
Can this be done quickly in jQuery before I start looping through them? Or
Can this be done? It seems like this should be possible. In general, I
Can this be done in JavaScript? type == 1 ? function1() : function2();
Can this be simplified to a one liner? Feel free to completely rewrite it

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.