<bdo id='gSdj1'></bdo><ul id='gSdj1'></ul>

    <tfoot id='gSdj1'></tfoot>
  • <legend id='gSdj1'><style id='gSdj1'><dir id='gSdj1'><q id='gSdj1'></q></dir></style></legend>
    1. <small id='gSdj1'></small><noframes id='gSdj1'>

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

        在 C# 中处理整数溢出的最佳方法?

        时间:2023-07-25
        <tfoot id='JDrLu'></tfoot>
          <tbody id='JDrLu'></tbody>

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

        • <bdo id='JDrLu'></bdo><ul id='JDrLu'></ul>

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

                  <legend id='JDrLu'><style id='JDrLu'><dir id='JDrLu'><q id='JDrLu'></q></dir></style></legend>
                  本文介绍了在 C# 中处理整数溢出的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  处理整数溢出是一项常见任务,但在 C# 中处理它的最佳方法是什么?是否有一些语法糖使它比其他语言更简单?或者这真的是最好的方法吗?

                  Handling integer overflow is a common task, but what's the best way to handle it in C#? Is there some syntactic sugar to make it simpler than with other languages? Or is this really the best way?

                  int x = foo();
                  int test = x * common;
                  if(test / common != x)
                      Console.WriteLine("oh noes!");
                  else
                      Console.WriteLine("safe!");
                  

                  推荐答案

                  我不需要经常使用这个,但是你可以使用 checked 关键字:

                  I haven't needed to use this often, but you can use the checked keyword:

                  int x = foo();
                  int test = checked(x * common);
                  

                  如果溢出将导致运行时异常.来自 MSDN:

                  Will result in a runtime exception if overflows. From MSDN:

                  在检查的上下文中,如果表达式产生的值是在目标类型范围之外,结果取决于表达式是常量还是非常量.持续的表达式导致编译时错误,而非常量表达式在运行时进行评估并引发异常.

                  In a checked context, if an expression produces a value that is outside the range of the destination type, the result depends on whether the expression is constant or non-constant. Constant expressions cause compile time errors, while non-constant expressions are evaluated at run time and raise exceptions.

                  我还应该指出,还有另一个 C# 关键字,unchecked,它当然与 checked 相反并且忽略溢出.您可能想知道您何时使用过 unchecked,因为它似乎是默认行为.好吧,有一个 C# 编译器选项定义了如何处理 checkedunchecked 之外的表达式:/checked.您可以在项目的高级构建设置下进行设置.

                  I should also point out that there is another C# keyword, unchecked, which of course does the opposite of checked and ignores overflows. You might wonder when you'd ever use unchecked since it appears to be the default behavior. Well, there is a C# compiler option that defines how expressions outside of checked and unchecked are handled: /checked. You can set it under the advanced build settings of your project.

                  如果您有很多表达式需要检查,最简单的做法实际上是设置 /checked 构建选项.那么任何溢出的表达式,除非包含在 unchecked 中,都会导致运行时异常.

                  If you have a lot of expressions that need to be checked, the simplest thing to do would actually be to set the /checked build option. Then any expression that overflows, unless wrapped in unchecked, would result in a runtime exception.

                  这篇关于在 C# 中处理整数溢出的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:LINQ to Entities 无法识别方法“Int32 Parse(System.String)"方法,并 下一篇:比较 Double 和 Int 的最佳方法是什么?

                  相关文章

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

                  <legend id='VE4a7'><style id='VE4a7'><dir id='VE4a7'><q id='VE4a7'></q></dir></style></legend>
                    <bdo id='VE4a7'></bdo><ul id='VE4a7'></ul>
                  1. <small id='VE4a7'></small><noframes id='VE4a7'>