as 和 is的区别
来源:互联网 发布:unity3d开发的游戏案例 编辑:程序博客网 时间:2024/04/30 04:08
We’ve all had to do some casting in the past, right? Everyone knows how to write a cast in C#: you just indicate use the ( ) operate, stating the type you want and that’s it. Here’s an example:
var aux = (MyClass)derived;
The previous code will compile, but you might end up getting an exception if derive isn’t a MyClass instance (or derived from it or if you don’t define aconversion operator in your derived’s class). So, how can youcheck if an object is of a specific type without getting an exception? Enter the is and as operators! The is operator will never throw an exception and it will return a Boolean which indicates if the object is of a specific type:
var isDerived = derived is MyClass;
if( isDerived ){
//do something but cast before
//ex.: ((MyClass)derived).CallSomeMethod();
}
You can use this, but I’m guessing that what you’d really love to have is a cast which doesn’t throw an exception when the type you’re casting to isn’t “compatible”. Enter the as operator:
var auxDerived = derived as MyClass;
if( auxDerived != null ){
//do something
//no need for casting
//auxDerived.CallSomeMethod();
}
As you can see, the as operator will try to cast the object into the desired type. When that is possible, the result will automatically point to an instance of that type. When it isn’t, you’ll get null (that’s why you generally test the result of the operator against null before writing the code that accesses the members of the desired type).
Now, before you forget that the is operator exists, take a look at the following snippet:
public struct MyStruct { //more code }
var aux = instance as MyStruct;
Do you see anything wrong? No? Ok, let’s take a look at the docs:
Note that the as operator only performs reference conversions and boxing conversions. Theas operator cannot perform other conversions, such as user-defined conversions, which should instead be performed using cast expressions.
In other words, the previous snippet won’t compile because MyStruct is a struck. In fact, if you do read thehttp://msdn.microsoft.com/en-us/library/cscsdfbt(v=vs.100).aspx, then you’ll notice that it says that:
The as operator is like a cast except that it yields null on conversion failure instead of raising an exception. More formally, an expression of the form:
expression as typeis equivalent to:
expression is type ? (type)expression : (type)null
In fact, you’ll be able to program without knowing this (since the compiler will enforce it at compilation time. However, you’ll really be amazed by the number of guys that says that they’re an “expert” in C# and don’t know about this behavior. And did I said that there was one C# “expert” that told me there was no way to “cast” a type into another if there wasn’t a relationship of is-a between them? I guess he didn’t had the time to read the section onhttp://msdn.microsoft.com/en-us/library/85w54y0a.aspx in the C# spec
And that’s it for now. Stay tuned for more.
- is 和 as 的区别
- as 和 is的区别
- IS和AS的区别
- is和as的区别
- c# is和as的区别
- c# is和as的区别
- c# is和as的区别
- Oracle中的AS和IS的区别
- C#中is 和 as 的区别
- c# is和as的区别
- c# is和as的区别
- c# is和as的区别
- c# as is 的区别
- C# Is AS 的区别
- is 和 as 运算符的作用与区别 c#
- c# 中is和as关键字的区别
- c# 中 is 和 as 关键字的区别
- .NET 里as 与is 的区别和用途
- 分布式系统漫谈一 —— Google三驾马车: GFS,mapreduce,Bigtable
- 毕业之后的路
- 在 Linux 系统中安装Load Generator ,并在windows 调用
- 【黑马程序员】多线程学习日记1
- android软件更新方法初探
- as 和 is的区别
- 中国传媒大学数字信号处理精品课程
- windows多线程
- mysql的JDBC连接
- HDU 1010 Tempter of the Bone
- 【Linux 驱动】第五章 并发和竞态
- 第十二章 取样问题
- 我的时间管理——我的时间我做主之《一机在手,日程无忧》
- [转]openwrt网上资料