as 和 is的区别
2012-04-12 19:44
162 查看
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 a
conversion operator in your derived’s class). So, how can you
check 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. The
as 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 the
http://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:
is equivalent to:
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 on
http://msdn.microsoft.com/en-us/library/85w54y0a.aspx in the C# spec
And that’s it for now. Stay tuned for more.
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 a
conversion operator in your derived’s class). So, how can you
check 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. The
as 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 the
http://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 type
is 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 on
http://msdn.microsoft.com/en-us/library/85w54y0a.aspx in the C# spec
And that’s it for now. Stay tuned for more.
相关文章推荐
- C#中 is 和 as 的区别
- Oracle中存储过程和函数中IS和AS的区别
- oracle存储过程中is和as区别
- is和as的区别
- as 和is的区别
- 基础知识--is & as 的区别
- 记SQL语句中的as和is的区别
- is与as的区别
- 【C#语法】C#中强转和as、is的区别
- (转)AS3中 is,as,typeof的区别 .
- C#中is与as关键字的区别
- c#之is和as的区别用法
- .net中as和is之间的区别分析
- CLR via 笔记4.2 类型转换 is 与 as 区别
- C# is\as与强制转换的区别+值类型与引用类型转换区别
- Oracle存储过程中is和as区别
- 建议3: 区别对待强制转型与as和is
- .net中as和is之间的区别分析
- oracle pl/sql创建子程序时 as和is关键字的区别
- C#的IS和AS运算符区别