关于“as”和“is”的问题(转载)
是不是很多人不用c#中的using和as操作符?甚至不知道?
其实这2个操作符在小处非常有用。
1、using
按照msdn的解释
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
举例:
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467635.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467905.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467238.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467905.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467770.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467905.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467905.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467238.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467905.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467770.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269468661.gif)
调用这个类:
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467635.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467905.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269468661.gif)
可以看到先后输出了Do a method和Dispose。
备注:实例化的对象必须实现 System.IDisposable 接口
2、as
msdn这么说:
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
举例:
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467635.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467905.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467905.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269468661.gif)
这样的代码在转换类型失败的时候引发异常,代码修改成:
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467600.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467635.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467905.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269467905.gif)
![](http://www.webasp.net/article/uploadfile/200505/20055269468661.gif)
备注:as必须和引用类型一起使用(int等值类型不能使用)