<?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: A downcasting tragedy</title>
	<atom:link href="http://lostechies.com/jimmybogard/2007/11/14/a-downcasting-tragedy/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/jimmybogard/2007/11/14/a-downcasting-tragedy/</link>
	<description>Strong opinions, weakly held</description>
	<lastBuildDate>Wed, 22 May 2013 13:39:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<item>
		<title>By: Jimmy Bogard</title>
		<link>http://lostechies.com/jimmybogard/2007/11/14/a-downcasting-tragedy/#comment-59</link>
		<dc:creator>Jimmy Bogard</dc:creator>
		<pubDate>Thu, 15 Nov 2007 21:15:57 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2007/11/14/a-downcasting-tragedy.aspx#comment-59</guid>
		<description>What I&#039;m trying to do is make a decision based on the type.  Using the &quot;as&quot; operator, then checking for null, is the appropriate way to do this.

The InvalidCastException isn&#039;t really the problem, it&#039;s the downcasting and underlying assumptions.

This happens a lot if you&#039;re using the 1.1 HashTable class, or reflection.  Lots of things come out as &quot;object&quot;, and you need to use the &quot;as&quot; operator to try and coerce the object to a type.  If it doesn&#039;t work, fine, no exception thrown, and you can handle that case any way you like (including throwing an exception).

Bottom line is I don&#039;t want to rely on exceptions for flow control.  I&#039;m not suggesting replacing every cast operator to &quot;as&quot;, but it&#039;s usually about a 10:1 ratio or more.  I found that before I used &quot;as&quot;, I would have many &quot;try...catch (InvalidCastException)...finally&quot; blocks, that were better expressed with guard clauses and the &quot;as&quot; operator.</description>
		<content:encoded><![CDATA[<p>What I&#8217;m trying to do is make a decision based on the type.  Using the &#8220;as&#8221; operator, then checking for null, is the appropriate way to do this.</p>
<p>The InvalidCastException isn&#8217;t really the problem, it&#8217;s the downcasting and underlying assumptions.</p>
<p>This happens a lot if you&#8217;re using the 1.1 HashTable class, or reflection.  Lots of things come out as &#8220;object&#8221;, and you need to use the &#8220;as&#8221; operator to try and coerce the object to a type.  If it doesn&#8217;t work, fine, no exception thrown, and you can handle that case any way you like (including throwing an exception).</p>
<p>Bottom line is I don&#8217;t want to rely on exceptions for flow control.  I&#8217;m not suggesting replacing every cast operator to &#8220;as&#8221;, but it&#8217;s usually about a 10:1 ratio or more.  I found that before I used &#8220;as&#8221;, I would have many &#8220;try&#8230;catch (InvalidCastException)&#8230;finally&#8221; blocks, that were better expressed with guard clauses and the &#8220;as&#8221; operator.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mbratton</title>
		<link>http://lostechies.com/jimmybogard/2007/11/14/a-downcasting-tragedy/#comment-58</link>
		<dc:creator>mbratton</dc:creator>
		<pubDate>Thu, 15 Nov 2007 20:35:04 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2007/11/14/a-downcasting-tragedy.aspx#comment-58</guid>
		<description>Would it not be better to get the invalidcastexception at the point of failure and be aware of the actual problem in your code, rather than have it silently fail and possibly raise a NullReferenceException? I understand that you&#039;re saying to check for null, but to me that seems to sidestep the actual problem in this scenario, which is that the property&#039;s type is not expressed as its base.</description>
		<content:encoded><![CDATA[<p>Would it not be better to get the invalidcastexception at the point of failure and be aware of the actual problem in your code, rather than have it silently fail and possibly raise a NullReferenceException? I understand that you&#8217;re saying to check for null, but to me that seems to sidestep the actual problem in this scenario, which is that the property&#8217;s type is not expressed as its base.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
