快讯

初探c#(七)语句(Statements)

2004-02-14 09:34  出处:PConline  作者:依栏望海  责任编辑:pjl 

再看一个例子:*/ using System; class Test { const int x = 1000000; const int y = 1000000; static int F() { checked {return (x * y);} // 编译器警告(Compile warning):溢出(overflow) } static int G() { unchecked {return (x * y);} // 返回 -727379968 } static int H() { return x * y; // 编译器警告(Compile warning):溢出(overflow) } static void Main() { Console.WriteLine(F()); //可以注销掉此行试试。 Console.WriteLine(G()); Console.WriteLine(H()); //可以注销掉此行试试。 } } /* 当F()和H()求值的时候,就会引起一个编译警告。而在G()中,因为有了“unchecked”,屏蔽了这个警 告。要注意的是“checked”和“unchecked”都不能对函数的返回值进行操作!比如:*/ class Test { static int Multiply(int x, int y) { return x * y; } static int F() { checked{ return Multiply(1000000, 1000000); } // 与 return Multiply(1000000, 1000000); } // 有相同的效果。 } /* 其实大家稍微想一下知道为什么m$没有这么做!对这个内容的讨论超出本文的范围和俺的能力之外哦。   在c#中,所有的十六进制数都是uint。如果用强制类型转换会引起编译器报错。用“unchecked”则可以跳过这个机制,把uint的十六进制数转化为int。如:*/ class Test { public const int AllBits = unchecked((int)0xFFFFFFFF); public const int HighBit = unchecked((int)0x80000000); } /* 上例所有的常数都是uint,而且超过了int的范围,没有“unchecked”,这种转换会引发一个编译器错误。注意:上面用的是“unchecked”操作符。不是语句。不过它们之间除了一个用“()”,另一个用“{}”以外,几乎一样。BTW,“checked”同样。 1.7.16 “lock”语句(The lock statement) “lock”获得一个相互排斥的对象锁定。(俺查过一些资料,但都没有清晰说明,暂不介绍)
键盘也能翻页,试试“← →”键
IT热词搜索 来源:360新闻
软件论坛帖子排行
相关文章

相关软件:

腾讯QQ2012
大小:52.93 MB 授权:免费
腾讯QQ2012
立即下载
腾讯QQ2013
大小:49.32 MB 授权:免费
腾讯QQ2013
立即下载