<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>
<channel>
	<title>Comments on: Cast abuse</title>
	<atom:link href="http://www.chrishowie.com/2007/06/12/cast-abuse/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chrishowie.com/2007/06/12/cast-abuse/</link>
	<description>Trading social skills for technical prowess since 1994</description>
	<pubDate>Tue, 06 Jan 2009 12:59:07 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Jacob</title>
		<link>http://www.chrishowie.com/2007/06/12/cast-abuse/comment-page-1/#comment-7022</link>
		<dc:creator>Jacob</dc:creator>
		<pubDate>Tue, 04 Mar 2008 23:51:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrishowie.com/2007/06/12/cast-abuse/#comment-7022</guid>
		<description>Perhaps DoStuff is implemented as an extension method on Widget that safely does nothing when Widget is null... :)</description>
		<content:encoded><![CDATA[<p>Perhaps DoStuff is implemented as an extension method on Widget that safely does nothing when Widget is null&#8230; <img src='http://www.chrishowie.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://www.chrishowie.com/2007/06/12/cast-abuse/comment-page-1/#comment-816</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Wed, 13 Jun 2007 01:03:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrishowie.com/2007/06/12/cast-abuse/#comment-816</guid>
		<description>As a side note, the IL generated by "((Foo) o).Bar()" and "(o as Foo).Bar()" are identical in size and only differ in that the former uses "castclass Foo" and the latter "isinst Foo".  A quick google suggests that performance is identical when the cast succeeds.

As a matter of coding style though, I still say using "as" here is putting a square peg in a round hole.</description>
		<content:encoded><![CDATA[<p>As a side note, the IL generated by &#8220;((Foo) o).Bar()&#8221; and &#8220;(o as Foo).Bar()&#8221; are identical in size and only differ in that the former uses &#8220;castclass Foo&#8221; and the latter &#8220;isinst Foo&#8221;.  A quick google suggests that performance is identical when the cast succeeds.</p>
<p>As a matter of coding style though, I still say using &#8220;as&#8221; here is putting a square peg in a round hole.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aaron Bockover</title>
		<link>http://www.chrishowie.com/2007/06/12/cast-abuse/comment-page-1/#comment-815</link>
		<dc:creator>Aaron Bockover</dc:creator>
		<pubDate>Tue, 12 Jun 2007 22:41:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrishowie.com/2007/06/12/cast-abuse/#comment-815</guid>
		<description>I often do this if I need to do a check+cast for only a single operation:

if(o is Something) {
   ((Something)o).Blah();
}

If I intend to do multiple operations after a cast, it's nice to use 'as':

Something something = o as Something;
if(o != null) {
    o.Blah();
    o.Hooray();
}

It's really useful when you are working with an object that implements many interfaces and you want to know if it's ok to call a method from some interface. The 'is' will let you know if your cast is safe, or the 'as' will do the checking and the cast if it's safe.

Unless you're sure 'as' will not return null, or if it does and you do a check for null, be extremely careful with NullReferenceException (NRE). You basically _never_ want to have an NRE raised on you - ever. 

Under the hood, an NRE is just a sign that the JIT tried to dereference a native null pointer, which in turn causes a SIGSEGV (segfault). The runtime however traps the SIGSEGV signal so it can translate it into the NRE and send it upstream to your managed application as an exception. 

All this meaning that NREs are expensive and also can be prone to break your application if Mono mingles with some buggy library. If some library in your application traps SIGSEGV and doesn't restore the previous signal handler, Mono will never be able to translate that SIGSEGV into an NRE and your application will crash for no apparent reason, and with no easy way to trace and find out why. 

This was a big bug in GStreamer a few versions back where it trapped SIGSEGV during its plugin loading process but restored it to the system default handler and not the previous handler (Mono's JIT), thus Mono became unable to recover from the SIGSEGV.

/me ends rant :-)</description>
		<content:encoded><![CDATA[<p>I often do this if I need to do a check+cast for only a single operation:</p>
<p>if(o is Something) {<br />
   ((Something)o).Blah();<br />
}</p>
<p>If I intend to do multiple operations after a cast, it&#8217;s nice to use &#8216;as&#8217;:</p>
<p>Something something = o as Something;<br />
if(o != null) {<br />
    o.Blah();<br />
    o.Hooray();<br />
}</p>
<p>It&#8217;s really useful when you are working with an object that implements many interfaces and you want to know if it&#8217;s ok to call a method from some interface. The &#8216;is&#8217; will let you know if your cast is safe, or the &#8216;as&#8217; will do the checking and the cast if it&#8217;s safe.</p>
<p>Unless you&#8217;re sure &#8216;as&#8217; will not return null, or if it does and you do a check for null, be extremely careful with NullReferenceException (NRE). You basically _never_ want to have an NRE raised on you - ever. </p>
<p>Under the hood, an NRE is just a sign that the JIT tried to dereference a native null pointer, which in turn causes a SIGSEGV (segfault). The runtime however traps the SIGSEGV signal so it can translate it into the NRE and send it upstream to your managed application as an exception. </p>
<p>All this meaning that NREs are expensive and also can be prone to break your application if Mono mingles with some buggy library. If some library in your application traps SIGSEGV and doesn&#8217;t restore the previous signal handler, Mono will never be able to translate that SIGSEGV into an NRE and your application will crash for no apparent reason, and with no easy way to trace and find out why. </p>
<p>This was a big bug in GStreamer a few versions back where it trapped SIGSEGV during its plugin loading process but restored it to the system default handler and not the previous handler (Mono&#8217;s JIT), thus Mono became unable to recover from the SIGSEGV.</p>
<p>/me ends rant <img src='http://www.chrishowie.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anthony Cowley</title>
		<link>http://www.chrishowie.com/2007/06/12/cast-abuse/comment-page-1/#comment-814</link>
		<dc:creator>Anthony Cowley</dc:creator>
		<pubDate>Tue, 12 Jun 2007 19:52:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrishowie.com/2007/06/12/cast-abuse/#comment-814</guid>
		<description>Yeah, this is an interesting one. I think it's really just because the syntax looks nice. What happens is that you have cases where you're sure the cast will succeed, so you use "as" instead of a parenthetical cast just for the prettier syntax. I don't think there's really anything wrong with that usage, except that it establishes a pattern of using "as" that is different from the correct usage pattern when the cast may fail.</description>
		<content:encoded><![CDATA[<p>Yeah, this is an interesting one. I think it&#8217;s really just because the syntax looks nice. What happens is that you have cases where you&#8217;re sure the cast will succeed, so you use &#8220;as&#8221; instead of a parenthetical cast just for the prettier syntax. I don&#8217;t think there&#8217;s really anything wrong with that usage, except that it establishes a pattern of using &#8220;as&#8221; that is different from the correct usage pattern when the cast may fail.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://www.chrishowie.com/2007/06/12/cast-abuse/comment-page-1/#comment-812</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Tue, 12 Jun 2007 13:17:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrishowie.com/2007/06/12/cast-abuse/#comment-812</guid>
		<description>Ramon:

Yes, I'm not saying that you should not use "as."  I'm saying you shouldn't do it when doing a one-shot cast like "(o as Foo).DoSomething()" because if o can't be converted to Foo the exception you get (NullReferenceException) will be very misleading.</description>
		<content:encoded><![CDATA[<p>Ramon:</p>
<p>Yes, I&#8217;m not saying that you should not use &#8220;as.&#8221;  I&#8217;m saying you shouldn&#8217;t do it when doing a one-shot cast like &#8220;(o as Foo).DoSomething()&#8221; because if o can&#8217;t be converted to Foo the exception you get (NullReferenceException) will be very misleading.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramon</title>
		<link>http://www.chrishowie.com/2007/06/12/cast-abuse/comment-page-1/#comment-811</link>
		<dc:creator>Ramon</dc:creator>
		<pubDate>Tue, 12 Jun 2007 12:42:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrishowie.com/2007/06/12/cast-abuse/#comment-811</guid>
		<description>@Chris: Yes you are right but what I meant is that getting a null reference as input parameter is almost always incorrect and that you should first validatie that. While it is true that it will not result in an error with the 'as' or 'is' keywords it is probably not intended (in most scenarios) to be used that way.

Besides that.. my if scenario results in worse performance because the runtime now checks 'o' twice while with the 'as' keyword this is only done once.</description>
		<content:encoded><![CDATA[<p>@Chris: Yes you are right but what I meant is that getting a null reference as input parameter is almost always incorrect and that you should first validatie that. While it is true that it will not result in an error with the &#8216;as&#8217; or &#8216;is&#8217; keywords it is probably not intended (in most scenarios) to be used that way.</p>
<p>Besides that.. my if scenario results in worse performance because the runtime now checks &#8216;o&#8217; twice while with the &#8216;as&#8217; keyword this is only done once.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://www.chrishowie.com/2007/06/12/cast-abuse/comment-page-1/#comment-810</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Tue, 12 Jun 2007 12:33:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrishowie.com/2007/06/12/cast-abuse/#comment-810</guid>
		<description>Ramon:

Actually that test will catch it, because the "is" operator always returns false when the left operand is null.  "o is MyObject" is asking the question "is o not null and can it be converted to MyObject?"

Note that null is special in this case because while "is" will always return false, a cast to anything (except for a value type) will succeed.</description>
		<content:encoded><![CDATA[<p>Ramon:</p>
<p>Actually that test will catch it, because the &#8220;is&#8221; operator always returns false when the left operand is null.  &#8220;o is MyObject&#8221; is asking the question &#8220;is o not null and can it be converted to MyObject?&#8221;</p>
<p>Note that null is special in this case because while &#8220;is&#8221; will always return false, a cast to anything (except for a value type) will succeed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramon</title>
		<link>http://www.chrishowie.com/2007/06/12/cast-abuse/comment-page-1/#comment-809</link>
		<dc:creator>Ramon</dc:creator>
		<pubDate>Tue, 12 Jun 2007 12:25:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrishowie.com/2007/06/12/cast-abuse/#comment-809</guid>
		<description>:-) You are right about that. Well that is because almost nobody seam to understand casting. Maybe these where VB programmers before they came to .net.

The same as retrieving objects from an untyped dataset :-)

DataRow dr = ds.Tables[0].Rows[0];
Convert.ToInt32(dr[0]);

This is fairly common while it should of course be:

DataRow dr = ds.Tables[0].Rows[0];
(int)dr[0];


And yet another example of the 'as' keyword

You'll see this quite often too:

if(o is MyOjbect)
{
   MyObject mo = (MyObject)o;
}

While your method should be used by comparing the result against null. Well, first of all check that the existing reference isn't null :-)</description>
		<content:encoded><![CDATA[<p> <img src='http://www.chrishowie.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> You are right about that. Well that is because almost nobody seam to understand casting. Maybe these where VB programmers before they came to .net.</p>
<p>The same as retrieving objects from an untyped dataset <img src='http://www.chrishowie.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>DataRow dr = ds.Tables[0].Rows[0];<br />
Convert.ToInt32(dr[0]);</p>
<p>This is fairly common while it should of course be:</p>
<p>DataRow dr = ds.Tables[0].Rows[0];<br />
(int)dr[0];</p>
<p>And yet another example of the &#8216;as&#8217; keyword</p>
<p>You&#8217;ll see this quite often too:</p>
<p>if(o is MyOjbect)<br />
{<br />
   MyObject mo = (MyObject)o;<br />
}</p>
<p>While your method should be used by comparing the result against null. Well, first of all check that the existing reference isn&#8217;t null <img src='http://www.chrishowie.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Asbjørn Ulsberg</title>
		<link>http://www.chrishowie.com/2007/06/12/cast-abuse/comment-page-1/#comment-808</link>
		<dc:creator>Asbjørn Ulsberg</dc:creator>
		<pubDate>Tue, 12 Jun 2007 12:12:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrishowie.com/2007/06/12/cast-abuse/#comment-808</guid>
		<description>Yea, I too think that a lot of programmers don't realize the difference. Plus, checking for "null" is something most programmers never do (no matter what). It's great that you're pointing it out, though. :)</description>
		<content:encoded><![CDATA[<p>Yea, I too think that a lot of programmers don&#8217;t realize the difference. Plus, checking for &#8220;null&#8221; is something most programmers never do (no matter what). It&#8217;s great that you&#8217;re pointing it out, though. <img src='http://www.chrishowie.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jerry</title>
		<link>http://www.chrishowie.com/2007/06/12/cast-abuse/comment-page-1/#comment-807</link>
		<dc:creator>Jerry</dc:creator>
		<pubDate>Tue, 12 Jun 2007 12:09:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrishowie.com/2007/06/12/cast-abuse/#comment-807</guid>
		<description>Let me clarify... the use has migrated from Delphi, Anders clearly implemented it differently in C#. It must be late... sorry for the confusion.</description>
		<content:encoded><![CDATA[<p>Let me clarify&#8230; the use has migrated from Delphi, Anders clearly implemented it differently in C#. It must be late&#8230; sorry for the confusion.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
