<?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: Casting with &#8216;as&#8217; in C#</title>
	<atom:link href="http://blog.mattwynne.net/2007/09/04/casting-with-as-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mattwynne.net/2007/09/04/casting-with-as-in-c/</link>
	<description>Matt Wynne taking it one tea at a time</description>
	<lastBuildDate>Fri, 27 Jan 2012 08:31:34 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Derek</title>
		<link>http://blog.mattwynne.net/2007/09/04/casting-with-as-in-c/comment-page-1/#comment-548</link>
		<dc:creator>Derek</dc:creator>
		<pubDate>Fri, 15 May 2009 19:31:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mattwynne.net/2007/09/04/casting-with-as-in-c/#comment-548</guid>
		<description>&lt;p&gt;I just happened to find this explanation on the same issue:
http://blog.nerdbank.net/2008/06/when-not-to-use-c-keyword.html&lt;/p&gt;

&lt;p&gt;The author shows that using &quot;as&quot; only returns a &quot;NullReferenceException&quot;, where a &quot;()&quot; cast returns an &quot;InvalidCastException&quot; and is a more useful error during debugging.  But, he also shows where the &quot;as&quot; could be useful, since it silently returns a null value on failure.&lt;/p&gt;

&lt;p&gt;Personally, I&#039;ll take a slightly less &quot;readable&quot; line over potential irritation during debugging!&lt;/p&gt;

&lt;p&gt;-Derek&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I just happened to find this explanation on the same issue:
<a href="http://blog.nerdbank.net/2008/06/when-not-to-use-c-keyword.html" rel="nofollow">http://blog.nerdbank.net/2008/06/when-not-to-use-c-keyword.html</a></p>

<p>The author shows that using &#8220;as&#8221; only returns a &#8220;NullReferenceException&#8221;, where a &#8220;()&#8221; cast returns an &#8220;InvalidCastException&#8221; and is a more useful error during debugging.  But, he also shows where the &#8220;as&#8221; could be useful, since it silently returns a null value on failure.</p>

<p>Personally, I&#8217;ll take a slightly less &#8220;readable&#8221; line over potential irritation during debugging!</p>

<p>-Derek</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://blog.mattwynne.net/2007/09/04/casting-with-as-in-c/comment-page-1/#comment-11</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Tue, 11 Sep 2007 16:51:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mattwynne.net/2007/09/04/casting-with-as-in-c/#comment-11</guid>
		<description>&lt;p&gt;I have to agree with Mark on all-points!&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I have to agree with Mark on all-points!</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://blog.mattwynne.net/2007/09/04/casting-with-as-in-c/comment-page-1/#comment-10</link>
		<dc:creator>Matt</dc:creator>
		<pubDate>Tue, 04 Sep 2007 20:51:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mattwynne.net/2007/09/04/casting-with-as-in-c/#comment-10</guid>
		<description>&lt;p&gt;Great points and info Mark, and who am I to argue with the mighty Box, eh?&lt;/p&gt;

&lt;p&gt;&lt;p&gt;Don&#039;t get me wrong. I&#039;m not for a moment advocating throwing an exception as part of any expected production execution path. I can&#039;t imagine wanting to... so why worry about the &#039;cost&#039;?&lt;/p&gt;
&lt;p&gt;The value of a well-placed exception though, for me, is that it identifies clearly to any consumer or modifier of a method that it has violated some contract. In other words, the programmer has made a mistake.&lt;/p&gt;
&lt;p&gt;In this case, the code we&#039;re working on takes a &#039;widgetId&#039; (I should have called the parameter dooferId) which &lt;em&gt;must&lt;/em&gt; be the Id of a doofer for the subsequent lines of code in the method to be meaningful and work.&lt;/p&gt;
&lt;p&gt;We could re-write the first case like this:&lt;/p&gt;
&lt;pre&gt;Doofer doofer = GetWidget(dooferId) as Doofer;
if (doofer == null)
    throw new ArgumentException(&quot;dooferId&quot;,&quot;Must be the ID of a valid Doofer in the system&quot;);
&lt;/pre&gt;
&lt;p&gt;That works, and maybe it&#039;s more readable than my alternative. I&#039;d have been happy if I&#039;d seen that.&lt;/p&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Great points and info Mark, and who am I to argue with the mighty Box, eh?</p>

<p></p><p>Don&#8217;t get me wrong. I&#8217;m not for a moment advocating throwing an exception as part of any expected production execution path. I can&#8217;t imagine wanting to&#8230; so why worry about the &#8216;cost&#8217;?</p>
<p>The value of a well-placed exception though, for me, is that it identifies clearly to any consumer or modifier of a method that it has violated some contract. In other words, the programmer has made a mistake.</p>
<p>In this case, the code we&#8217;re working on takes a &#8216;widgetId&#8217; (I should have called the parameter dooferId) which <em>must</em> be the Id of a doofer for the subsequent lines of code in the method to be meaningful and work.</p>
<p>We could re-write the first case like this:</p>
<pre>Doofer doofer = GetWidget(dooferId) as Doofer;
if (doofer == null)
    throw new ArgumentException("dooferId","Must be the ID of a valid Doofer in the system");
</pre>
<p>That works, and maybe it&#8217;s more readable than my alternative. I&#8217;d have been happy if I&#8217;d seen that.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Mark</title>
		<link>http://blog.mattwynne.net/2007/09/04/casting-with-as-in-c/comment-page-1/#comment-9</link>
		<dc:creator>Mark</dc:creator>
		<pubDate>Tue, 04 Sep 2007 19:51:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mattwynne.net/2007/09/04/casting-with-as-in-c/#comment-9</guid>
		<description>&lt;p&gt;Interesting. I&#039;ve always followed the advice given by Don Box in Essential .NET, which is that the as operator (which translates to a CIL isinst instruction) should be preferred to a c# cast (which translates to a CIL castclass instruction) unless you know the cast will succeed because of the cost of exceptions.  If your code works specifically on doofers, I&#039;d recommend that you ask for them specifically - i.e. in a function that works specifically on doofers, the argument should be of type doofer.  That way it&#039;s the responsibility of the calling code to check it&#039;s going to pass you something you know you can use.  In the client code, if there&#039;s a chance that the cast will fail, you only need one test for null after using the as operator and off you go.  There is only a problem when you&#039;re downcasting (e.g. going from a base type to a specific subclass) or sidecasting (going from a type to an orthogonal type.)  For sidecasting, I prefer converters (e.g. ToString or Convert.ToInt32.)  In the case of downcasting, I think you have to consider why it is necessary at all - the usual case being retrieving from a list or some other container that is deliberately unfussy about type - and see if you can refactor (e.g. generics provide greater protection against this type of bug.)&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Interesting. I&#8217;ve always followed the advice given by Don Box in Essential .NET, which is that the as operator (which translates to a CIL isinst instruction) should be preferred to a c# cast (which translates to a CIL castclass instruction) unless you know the cast will succeed because of the cost of exceptions.  If your code works specifically on doofers, I&#8217;d recommend that you ask for them specifically &#8211; i.e. in a function that works specifically on doofers, the argument should be of type doofer.  That way it&#8217;s the responsibility of the calling code to check it&#8217;s going to pass you something you know you can use.  In the client code, if there&#8217;s a chance that the cast will fail, you only need one test for null after using the as operator and off you go.  There is only a problem when you&#8217;re downcasting (e.g. going from a base type to a specific subclass) or sidecasting (going from a type to an orthogonal type.)  For sidecasting, I prefer converters (e.g. ToString or Convert.ToInt32.)  In the case of downcasting, I think you have to consider why it is necessary at all &#8211; the usual case being retrieving from a list or some other container that is deliberately unfussy about type &#8211; and see if you can refactor (e.g. generics provide greater protection against this type of bug.)</p>]]></content:encoded>
	</item>
</channel>
</rss>

