I have a loop which loops up to 20.
I am using C# 4.0 and LINQ (Where necessary). What’s a good way to say
"if i > 3 && < 5"
but for various bands (eg between 3 and 5, 6, and 8, etc), without loads of if statements in the loops?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
If you just want to simplify syntax, you could write an extension method:
Of course, it is debatable if
i.IsBetween(3, 5)really is cleaner than the sample you have provided.If really you are dealing with “various bands”, you could use the efficient Interval Tree data-structure. If performance is not an issue, you could just write a simple class yourself:
This will allow you to write code like: