<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# 中处理整数溢出的最佳方法?

        Best way to handle Integer overflow in C#?(在 C# 中处理整数溢出的最佳方法?)
        <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# 中处理整数溢出的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 帮助器中提取显示名称和描述属性)
                  How can I force the PropertyGrid to show a custom dialog for a specific property?(如何强制 PropertyGrid 显示特定属性的自定义对话框?)
                  Associate attribute with code generated property in .net(将属性与 .net 中的代码生成属性相关联)
                    <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>
                        <tbody id='VE4a7'></tbody>
                      1. <small id='VE4a7'></small><noframes id='VE4a7'>