c# checked关键字
来源:互联网 发布:mysql安装教程 编辑:程序博客网 时间:2024/06/15 05:21
checked 关键字用于对整型算术运算和转换显式启用溢出检查。
此示例演示如何使用 checked 启用运行时溢出检查。
C#
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 关键字
- C#关键字checked
- c# checked关键字
- c# checked 关键字当属性
- C# explicit implicit关键字、checked关键字
- C# 用 checked 做字段,但checked是C#关键字
- C#使用checked关键字处理"溢出"错误
- C#使用checked关键字处理"溢出"错误
- C# checked和unchecked关键字与强制类型转换
- checked和unchecked关键字
- c# checked unchecked 关键字,在需要的时候请记得用他们两位
- c# checked unchecked 关键字,在需要的时候请记得用他们两位
- c# checked unchecked 关键字,在需要的时候请记得用他们两位
- c# checked unchecked 关键字,在需要的时候请记得用他们两位
- C#学习篇(八)Nullable类型、checked关键字、运算符重载
- C# checked 和 unchecked
- C# checked和unchecked
- checked (C# Reference)
- Py_BuildValue()返回NULL的原因及解决方法;string在char和utf-8编码之间互转
- Android 高版本API方法在低版本系统上的兼容性处理
- WebGIS中地图恢复初始位置及状态
- Java的封装
- 【Inno Setup】Gif插件2.1
- c# checked关键字
- 初步使用EventBus (一)
- python学习笔记1——python 中 Tkinder 使用常见问题
- LINUX分区、目录挂载
- Unity3D 快捷键技巧
- java图片等比例缩略显示在页面
- 怎么多了一个typename?
- 技术博客起源。
- Windows 2008R2上将VMware的虚拟机做成服务,实现开机自动启动虚拟机