码迷,mamicode.com
首页 > Windows程序 > 详细

C# 溢出检查

时间:2015-06-25 10:13:04      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

checked:

byte b = 255;
checked
{
b++;
}
Console.WriteLine(b.ToString());

执行出错:算术运算导致溢出。

unchecked:

byte b = 255;
unchecked
{
b++;
}
Console.WriteLine(b.ToString());

执行不出错:不检查是否溢出

 

is运算符

is运算符可以检查对象是否与特定的类型兼容

int i=10;

if(i is object)

{

  Console.WriteLine(true);

}

 

as运算符

as运算符用于执行引用类型的显式类型转换

object o1= "some string"`
object o2 = 5;
string s1 = o1 as string; // s1 = "sq1e string"
string s2 = o2 as string; // s2 = null

 

可空类型和运算符

int? a = null;
int? b = -5;
if (a >= b)
{
Console.WriteLine("a>=b");
}
else
{
Console.WriteLine("a<b");
}

C# 溢出检查

标签:

原文地址:http://www.cnblogs.com/hank-chen/p/4599167.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!