<small id='CywzC'></small><noframes id='CywzC'>

    <tfoot id='CywzC'></tfoot>
        <bdo id='CywzC'></bdo><ul id='CywzC'></ul>

      <legend id='CywzC'><style id='CywzC'><dir id='CywzC'><q id='CywzC'></q></dir></style></legend>

    1. <i id='CywzC'><tr id='CywzC'><dt id='CywzC'><q id='CywzC'><span id='CywzC'><b id='CywzC'><form id='CywzC'><ins id='CywzC'></ins><ul id='CywzC'></ul><sub id='CywzC'></sub></form><legend id='CywzC'></legend><bdo id='CywzC'><pre id='CywzC'><center id='CywzC'></center></pre></bdo></b><th id='CywzC'></th></span></q></dt></tr></i><div id='CywzC'><tfoot id='CywzC'></tfoot><dl id='CywzC'><fieldset id='CywzC'></fieldset></dl></div>

      如何优雅地检查一个数字是否在一个范围内?

      How to elegantly check if a number is within a range?(如何优雅地检查一个数字是否在一个范围内?)

        • <bdo id='sdZVX'></bdo><ul id='sdZVX'></ul>
          <tfoot id='sdZVX'></tfoot>
            <legend id='sdZVX'><style id='sdZVX'><dir id='sdZVX'><q id='sdZVX'></q></dir></style></legend>
              <i id='sdZVX'><tr id='sdZVX'><dt id='sdZVX'><q id='sdZVX'><span id='sdZVX'><b id='sdZVX'><form id='sdZVX'><ins id='sdZVX'></ins><ul id='sdZVX'></ul><sub id='sdZVX'></sub></form><legend id='sdZVX'></legend><bdo id='sdZVX'><pre id='sdZVX'><center id='sdZVX'></center></pre></bdo></b><th id='sdZVX'></th></span></q></dt></tr></i><div id='sdZVX'><tfoot id='sdZVX'></tfoot><dl id='sdZVX'><fieldset id='sdZVX'></fieldset></dl></div>

              <small id='sdZVX'></small><noframes id='sdZVX'>

                <tbody id='sdZVX'></tbody>

                本文介绍了如何优雅地检查一个数字是否在一个范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                如何使用 C# 优雅地做到这一点?

                How can I do this elegantly with C#?

                例如,一个数字可以介于 1 和 100 之间.

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

                我知道一个简单的 if (x >= 1 && x <= 100) 就足够了;但是随着大量语法糖和不断添加到 C#/.Net 中的新功能,这个问题是关于更惯用(一个可以是优雅)的编写方式.

                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.

                性能不是问题,但请在不是 O(1) 的解决方案中添加性能说明,因为人们可能会复制粘贴建议.

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

                推荐答案

                有很多选择:

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

                事实上,基本的 if 可以在第一次检查中以相反的顺序更优雅地编写:

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

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

                另外,请查看此 SO 帖子,了解正则表达式选项.

                Also, check out this SO post for regex options.

                注意事项:

                • LINQ 解决方案严格用于样式点 - 因为 Contains 迭代所有项目,它的复杂性是 O(range_size) 而不是 O(1),通常是范围检查所期望的.
                  其他范围的更通用版本(注意第二个参数是计数,而不是结束):

                • 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)
                

              • 没有&&这样的if解决方案是很有诱惑力的,比如1 <= x <= 100 -看起来很优雅,但在 C# 中会导致语法错误Operator '<=' cannot be applied to operands of type 'bool' and 'int'"

              • 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'"

                这篇关于如何优雅地检查一个数字是否在一个范围内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!
              • 相关文档推荐

                Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                Accessing attribute info from DTE(从 DTE 访问属性信息)
                c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                C# Attributes and their uses(C# 属性及其用途)
                C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)
              • <small id='SLm8k'></small><noframes id='SLm8k'>

                        <bdo id='SLm8k'></bdo><ul id='SLm8k'></ul>
                        <legend id='SLm8k'><style id='SLm8k'><dir id='SLm8k'><q id='SLm8k'></q></dir></style></legend>

                          <tbody id='SLm8k'></tbody>

                        • <i id='SLm8k'><tr id='SLm8k'><dt id='SLm8k'><q id='SLm8k'><span id='SLm8k'><b id='SLm8k'><form id='SLm8k'><ins id='SLm8k'></ins><ul id='SLm8k'></ul><sub id='SLm8k'></sub></form><legend id='SLm8k'></legend><bdo id='SLm8k'><pre id='SLm8k'><center id='SLm8k'></center></pre></bdo></b><th id='SLm8k'></th></span></q></dt></tr></i><div id='SLm8k'><tfoot id='SLm8k'></tfoot><dl id='SLm8k'><fieldset id='SLm8k'></fieldset></dl></div>
                        • <tfoot id='SLm8k'></tfoot>