If object is SomeType vs. object as SomeType

Possible Duplicates:
Direct casting vs 'as' operator?
casting vs using the 'as' keyword in the CLR

I know I can do the following:

if(someObject is Bar)
{
    Bar b = (Bar)someObject;
    //Use b
}

and also:

Bar b = someObject as Bar;
if(b != null)
{
    //Use b
}

Is one preferable over the other, if I assume I'm just going to reference the someObject in another variable?

EDIT: By preferable, I'm meaning following conventions, performance, chance for bugs, and readability.


The first version has two checks, whereas the second one has only one check.
--> The second one is preferable.

链接地址: http://www.djcxy.com/p/73748.html

上一篇: Java中的SoftReference和WeakReference有什么区别?

下一篇: 如果对象是SomeType而对象是SomeType