[–] jimfl 15y ago ↗ Hmm. 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 15y ago ↗ You can always use the null coalescing operator.(foo as Bar ?? new Bar()).Method();
[–] benJIman 15y ago ↗ You can always use the null coalescing operator.(foo as Bar ?? new Bar()).Method();
2 comments
[ 3.5 ms ] story [ 9.7 ms ] threadWhich makes the code example
dangerous without a guard.Edit: punctuated for clarity.
(foo as Bar ?? new Bar()).Method();