[mono-android] Not catching null reference - why?

Joel Low joel at joelsplace.sg
Wed Jun 27 23:20:28 UTC 2012


Yes, the ref as Type will return null if it cannot be converted to type
(somewhat akin to C++'s dynamic_cast, with some differences); I was
referring to the OP's code snippet: the explicit cast should fail and an
InvalidCastException should be thrown?

public static Bitmap DecodeFile(string _path)
       {
           WeakReference reference;
           var options = new BitmapFactory.Options {InPreferredConfig =
Bitmap.Config.Rgb565};
           Bitmap image=null;
           //refs is a dictionary of Dictionary<string, WeakReference>
           if (refs.TryGetValue(_path, out reference))
           {
                   if (reference.IsAlive)
                   {
                       image = (Bitmap)reference.Target; //
<<------------------- over here!

Regards,
Joel

> -----Original Message-----
> From: monodroid-bounces at lists.ximian.com [mailto:monodroid-
> bounces at lists.ximian.com] On Behalf Of Jonathan Pryor
> Sent: Wednesday, 27 June, 2012 9:47 PM
> To: Discussions related to Mono for Android
> Subject: Re: [mono-android] Not catching null reference - why?
> 
> On Jun 26, 2012, at 9:03 PM, Joel Low wrote:
> > Isn't the normal (= .NET) behaviour to throw an InvalidCastException in
this
> case?
> 
> No. The `value as Type` expression will either return null or return a
non-null
> instance of Type. Type cannot be a value type.
> 
> 	http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx
> 
>  - Jon
> 
> _______________________________________________
> Monodroid mailing list
> Monodroid at lists.ximian.com
> 
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 6547 bytes
Desc: not available
URL: <http://lists.ximian.com/pipermail/monodroid/attachments/20120628/bc56e5e9/attachment-0001.bin>


More information about the Monodroid mailing list