The as Operator(gimmecsharp.blogspot.com)2 points·by rueleonheart·vor 16 Jahren·2 commentsgimmecsharp.blogspot.comThe as Operatorhttp://gimmecsharp.blogspot.com/2010/08/as-operator.html1 commentsPost comment[–]jimfl·vor 16 JahrenreplyHmm. The article says that the as operator is the same as casting, which is not correct. Casting will result in an exception if coercion fails, where as 'as' will return a null reference.Which makes the code example (foo as Bar).Method(); dangerous without a guard.Edit: punctuated for clarity.[–]benJIman·vor 16 JahrenreplyYou can always use the null coalescing operator.(foo as Bar ?? new Bar()).Method();
Which makes the code example
dangerous without a guard.
Edit: punctuated for clarity.