checked(expression)与unchecked(expression)的区别C#中,关于类型转换的部分有一个问题不明白,那就是checked(expression)unchecked(expression)的区别,适用的情况,最好有小程序例子最好有示例,比如说checked(expres

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/03 19:54:49
checked(expression)与unchecked(expression)的区别C#中,关于类型转换的部分有一个问题不明白,那就是checked(expression)unchecked(expression)的区别,适用的情况,最好有小程序例子最好有示例,比如说checked(expres

checked(expression)与unchecked(expression)的区别C#中,关于类型转换的部分有一个问题不明白,那就是checked(expression)unchecked(expression)的区别,适用的情况,最好有小程序例子最好有示例,比如说checked(expres
checked(expression)与unchecked(expression)的区别
C#中,关于类型转换的部分有一个问题不明白,那就是
checked(expression)
unchecked(expression)
的区别,适用的情况,最好有小程序例子
最好有示例,比如说
checked(expression)
unchecked(expression)
分别使用,如果有溢出时,得到的结果是什么,如何使用等等.

checked(expression)与unchecked(expression)的区别C#中,关于类型转换的部分有一个问题不明白,那就是checked(expression)unchecked(expression)的区别,适用的情况,最好有小程序例子最好有示例,比如说checked(expres
short a = 32767;
short b = 32767;
short c = (short)(a + b);
Console.WriteLine(c);
结果c是-2,显然不对,关键是程序运行没提示错误,很可能处理结果bug出现.
try
{
short a = 32767;
short b = 32767;
short c = checked((short)(a + b));
Console.WriteLine(c);
}
catch (OverflowException e)
{
Console.WriteLine(e.Message);
}
用checked检查错误.