Possible Duplicates:
Direct casting vs 'as' operator?
Casting: (NewType) vs. Object as NewType
What is difference between normal typecasting and using “AS” keyword?
Possible Duplicates:
Direct casting vs 'as' operator?
Casting: (NewType) vs. Object as NewType
What is difference between normal typecasting and using “AS” keyword?
Using as
will fail gracefully if the object is the wrong type, and the resulting value will be null, where a normal cast would throw an InvalidCastException:
object x = new object();
string y = x as string; // y == null
string z = (string)x; // InvalidCastException
as
casting will nerer throw an exception, while usual cast can.
Object a = new Object();
String b = a as String;
if(b != null) // always false for this example.
{}
Normal type cast may return IllegalTypeCast exception where as as
will return null
in that case.
((Class2) obj) // Throws exception when the casting cannot be made
Class2 C = obj as Class2 // Will return NULL if the casting cannot be made
as
cannot be used with value types (non-nullable types).
For reference types...
expression as type
is really the same as
expression is type ? (type) expression : (type) null
except that expression
is evaluated only once.
In a nod to Bill Clinton, by 'is' in 'expression is type', I mean 'is'.
Basically, as the other answers indicate, that means that as returns null when the cast would fail. However, it also returns null when the cast would succeed but the type is wrong.
Here is a somewhat silly example:
uint? u = 52;
int? i = (int?) u; // 'i' is now 52
but
uint? u = 52;
object d = u;
int? i = d as int?;
What is the value of i? 52? Nope. It is null.
Why the object in there? Turns out I lied above when I explained 'as' in terms of 'is'.
Observe:
uint? u = 52;
int? i = (u is int?) ? (int?) u : (int?) null;
'i' is now null
uint? u = 52;
int? i = u as int?;
Oops. Compiler error. So, I guess the two statements are not exactly the same after all.
The use cases of the two operators are superficially similar, but semantically quite different. A cast communicates to the reader "I am certain that this conversion is legal and I am willing to take a runtime exception if I'm wrong". The "as" operator communicates "I don't know if this conversion is legal or not; we're going to give it a try and see how it goes".
For more on this topic see my article on the subject:
You will never get an converting exception if you using "as" keyword. The result of the convert will be null
if you are trying an invalid convert.
Implicitly/explicitly convert can be used between classes/interfaces with inherit/implement relations, otherwise it will give a compile error. See example:
public class A {}
public class B {}
...
A a = new A();
//B b = (B)a; //compile error:Cannot implicitly convert type 'A' to 'B'
Another situation of implicitly/explicitly convert is there is no relationship between class A and B, but you write an implicit/explicit operator yourself.
Also, as can only be used with reference types. This is actually quite logical, since if the conversion fails, it returns false, and that is not a possible value for a value type.
So for value types, normal typecasting must be used.