[Mono-dev] [PATCH] The big "as" cast cleanup

Rodrigo Kumpera kumpera at gmail.com
Tue Jan 8 09:25:55 EST 2008


Hi Juraj,

@@ -326,7 +324,7 @@
                         Console.Error.WriteLine ("WARNING: {0} is not
supported for now.", el.FirstChild.LocalName);
                         continue;
                     }
-                    XmlElement cpElem = el.SelectSingleNode ("cp") as
XmlElement;
+                    XmlElement cpElem = (XmlElement)el.SelectSingleNode
("cp");
                     string v = "";
                     if (cpElem != null)
                         v = new string ((char) (int.Parse (


Here there is an explicit test if cpElem is null, this could mean that the
code expect the cast to fail. Isn't that a possibility?


Rodrigo


On Jan 8, 2008 11:49 AM, Juraj Skripsky <js at hotfeet.ch> wrote:

> Hello,
>
> After getting annoyed by NullReferenceExceptions which turned out to be
> ClassCastExceptions in disguise, I've decided to clean up the code in
> mcs/class/corlib with regard to wrong/unnecessary uses of the "as" cast
> operator.
>
> Reasons for the clean up:
> -------------------------
> - NullRefExceptions are thrown where ClassCastException should be. With
> explicit casts, the later is thrown at the right spot and gives you more
> info.
> - Personally, I consider the use of the "as" operator to be confusing /
> irritating in those cases where a explicit cast is sufficient.
>
> The following links also describe the problems with the "as" operator:
>
> http://www.winterdom.com/weblog/2006/09/27/CastingOperatorsAndToString.aspx
> http://blog.mattwynne.net/2007/09/04/casting-with-as-in-c/
>
> The performance of explicit casts and "as" casts are pretty much the
> same on Mono and MS.NET (see and run attached test program).
>
> Attached is a patch (including ChangeLog entries).
> All unit tests pass.
>
> Please review.
>
> - Juraj
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20080108/bff6d8bd/attachment.html 


More information about the Mono-devel-list mailing list