C# checked与unchecked用法
来源:互联网 发布:cmseasy 知乎 编辑:程序博客网 时间:2024/06/10 01:10
checked 关键字用于对整型算术运算和转换显式启用溢出检查。
默认情况下,如果表达式仅包含常数值,且产生的值在目标类型范围之外,则它会导致编译器错误。 如果表达式包含一个或多个非常数值,则编译器不检测溢出。 在下面的示例中,计算赋给 i2 的表达式不会导致编译器错误。
// The following example causes compiler error CS0220 because 2147483647// is the maximum value for integers. //int i1 = 2147483647 + 10;// The following example, which includes variable ten, does not cause// a compiler error.int ten = 10;int i2 = 2147483647 + ten;// By default, the overflow in the previous statement also does// not cause a run-time exception. The following line displays // -2,147,483,639 as the sum of 2,147,483,647 and 10.Console.WriteLine(i2);
默认情况下,在运行时也不检查这些非常数表达式是否溢出,这些表达式不引发溢出异常。 上面的示例显示 -2,147,483,639 作为两个正整数之和。
可以通过编译器选项、环境配置或使用 checked 关键字来启用溢出检查。 下面的示例演示如何使用 checked 表达式或 checked 块,在运行时检测由前面的求和计算导致的溢出。 两个示例都引发溢出异常。
// If the previous sum is attempted in a checked environment, an
// OverflowException error is raised.
// Checked expression.
Console.WriteLine(checked(2147483647 + ten));
// Checked block.
checked
{
int i3 = 2147483647 + ten;
Console.WriteLine(i3);
}
可以使用unchecked取消溢出检查
此示例演示如何使用 checked 启用运行时溢出检查。
class OverFlowTest
{
// Set maxIntValue to the maximum value for integers.
static int maxIntValue = 2147483647;
// Using a checked expression.
static int CheckedMethod()
{
int z = 0;
try
{
// The following line raises an exception because it is checked.
z = checked(maxIntValue + 10);
}
catch (System.OverflowException e)
{
// The following line displays information about the error.
Console.WriteLine("CHECKED and CAUGHT: " + e.ToString());
}
// The value of z is still 0.
return z;
}
// Using an unchecked expression.
static int UncheckedMethod()
{
int z = 0;
try
{
// The following calculation is unchecked and will not
// raise an exception.
z = maxIntValue + 10;
}
catch (System.OverflowException e)
{
// The following line will not be executed.
Console.WriteLine("UNCHECKED and CAUGHT: " + e.ToString());
}
// Because of the undetected overflow, the sum of 2147483647 + 10 is
// returned as -2147483639.
return z;
}
static void Main()
{
Console.WriteLine("\nCHECKED output value is: {0}",
CheckedMethod());
Console.WriteLine("UNCHECKED output value is: {0}",
UncheckedMethod());
}
/*
Output:
CHECKED and CAUGHT: System.OverflowException: Arithmetic operation resulted
in an overflow.
at ConsoleApplication1.OverFlowTest.CheckedMethod()
CHECKED output value is: 0
UNCHECKED output value is: -2147483639
*/
}
0 0
- C# checked与unchecked用法
- C# checked与unchecked用法
- C# checked 和 unchecked
- C# checked和unchecked
- 《C#入门到精通》学习笔记 -- checked 与 unchecked
- C# checked和unchecked关键字与强制类型转换
- C# checked、unchecked操作符
- C#中的checked和unchecked
- C# checked、unchecked操作符
- C# checked、unchecked操作符
- C# checked和unchecked详解
- MENU关于CHECKED与UNCHECKED
- C#中的checked、unchecked操作符
- C#的checked和unchecked操作符
- C#的checked和unchecked操作符
- C# Keywords Series 3 checked&unchecked&decimal
- 自定义Checkbox的checked与unchecked
- 【摘抄】VB.NET中Checked与Unchecked
- QT中QWidget、QDialog及QMainWindow的区别
- delphi中获得当前时间
- android NDK jni下的c文件 Unresolved inclusion
- java代理模式 (转)
- java中的输入输出(转载)
- C# checked与unchecked用法
- 回溯与递归
- C# TextBox中的Validating与Validated事件
- C# string学习(转载)
- C# 中virtual,override,new 的区别与用法(转载)
- Jquery好友选择器
- Ubuntu 安装WineQQ2013SP6
- Aspxhtmleditior学习
- AspxhtmlEditor添加自定义按钮