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

Juraj Skripsky js at hotfeet.ch
Tue Jan 8 08:49:42 EST 2008


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: corlib_as_cleanup.patch
Type: text/x-patch
Size: 25643 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20080108/5a77c05e/attachment.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: performance_as_versus_explcast.cs
Type: text/x-csharp
Size: 635 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20080108/5a77c05e/attachment-0001.bin 


More information about the Mono-devel-list mailing list