Possible Duplicate:
Direct casting vs 'as' operator?
Casting vs. using the as keyword in the CLR
In C#, whats the difference
var obj = (Array)Something
vs
var obj = Something as Array
Possible Duplicate:
Direct casting vs 'as' operator?
Casting vs. using the as keyword in the CLR
In C#, whats the difference
var obj = (Array)Something
vs
var obj = Something as Array
first will throw a CastException if invalid. The second will only result in obj = null instead.
var obj = (Array)Something
will throw an InvalidCastExcpetion if Something
not derived from System.Array
or does not have a conversion operator for System.Array
. This can be used with value types and reference types.
var obj = Something as Array
will return null
(obj
will be null
) if Something
not derived from System.Array
or does not have a conversion operator for System.Array
. This can be used only with reference types. You'd need to box you value type first.
As well as Danijels answers
The first can be used with any type.
The as
operator can only be used on reference types.