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 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 onhttp://msdn.microsoft.com/en-us/library/85w54y0a.aspx in the C# spec

And that’s it for now. Stay tuned for more.