Beyond the regular boring difference between Cast
and As
- if i know that apple is a Fruit so I can use
(Fruit)apple
- and it throws an exception if it aint as value
can be checked against null to see if succeeded [won't throw Exception...]
However Ive been reading @EricLippert article about this and there was a nice sample about Nullable Value Types :
short? s = (short?)123;
int? i = s as int?;
this won't compile...
Cannot convert type 'short?' to 'int?' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
Fine.
so why this :
short? s = (short?)123;
int? i = (int?)s;
Does Compile ? ( Against ALL Expectations ! I KNOW that s
is not int?
- and it should go BANG but it aint ...)
the Cast checking here should be much more deadly than the former example (which went Bang)
I feel bad asking about this much-talked subject.
Thanks in Advance.