<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Who's Afraid of the big-bad cast operator?</title><link>http://blogs.msdn.com/cumgranosalis/archive/2008/03/24/who-s-afraid-of-the-big-bad-cast-operator.aspx</link><description>My previous blog entry (which was completely misunderstood by some people who commented - I blame the fact that English is not my native tongue) reminded me of another poor coding behavior. Just so we don't have a repeat of that last post, let me clarify</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Who's Afraid of the big-bad cast operator?</title><link>http://blogs.msdn.com/cumgranosalis/archive/2008/03/24/who-s-afraid-of-the-big-bad-cast-operator.aspx#8333872</link><pubDate>Mon, 24 Mar 2008 20:13:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8333872</guid><dc:creator>Mike</dc:creator><description>&lt;p&gt;Just a couple of days ago I saw &amp;quot;as&amp;quot; being used 4 times in 2 lines of code! Sheesh, especially that those 4 casts could have been reduced to just 2 but that's another story.&lt;/p&gt;
&lt;p&gt;As for why people use &amp;quot;as&amp;quot; instead of &amp;quot;is&amp;quot; you may be right that they are sort of afraid of cast exceptions but when I see &amp;quot;as&amp;quot; being used without a subsequent null check I tend to believed that they simply started writing code, realised that they need to cast something to get what they need and just slapped an &amp;quot;as&amp;quot; in.&lt;/p&gt;
</description></item><item><title>re: Who's Afraid of the big-bad cast operator?</title><link>http://blogs.msdn.com/cumgranosalis/archive/2008/03/24/who-s-afraid-of-the-big-bad-cast-operator.aspx#8336242</link><pubDate>Tue, 25 Mar 2008 21:24:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8336242</guid><dc:creator>Jeff Yates</dc:creator><description>&lt;p&gt;Thanks for blogging this. &amp;nbsp;It's been a battle on our project to highlight when and when not to use &amp;quot;as&amp;quot; and regular casts. &amp;nbsp;You've put it succinctly, although i think &amp;quot;is&amp;quot; requires mention.&lt;/p&gt;
&lt;p&gt;In my opinion, an &amp;quot;as&amp;quot; should be used if the code uses &amp;quot;is&amp;quot; followed by a cast if the &amp;quot;is&amp;quot; returns true, as the &amp;quot;as&amp;quot; does both these actions. If the value must ALWAYS be the type, a regular cast should be used; if the value doesn't matter and only the type does, the &amp;quot;is&amp;quot; should be used.&lt;/p&gt;
&lt;p&gt;Like I said, thanks. :)&lt;/p&gt;
</description></item><item><title>re: Who's Afraid of the big-bad cast operator?</title><link>http://blogs.msdn.com/cumgranosalis/archive/2008/03/24/who-s-afraid-of-the-big-bad-cast-operator.aspx#8336425</link><pubDate>Tue, 25 Mar 2008 22:57:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8336425</guid><dc:creator>Raj</dc:creator><description>&lt;p&gt;We use the following pattern a lot in our asp.net applications:&lt;/p&gt;
&lt;p&gt;Button btn = foo.FindControl(&amp;quot;Button&amp;quot;) as Button;&lt;/p&gt;
&lt;p&gt;if (btn != null) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; btn.Visible = false;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt; .. in places where the button is not guaranteed to exist.&lt;/p&gt;
</description></item><item><title>re: Who's Afraid of the big-bad cast operator?</title><link>http://blogs.msdn.com/cumgranosalis/archive/2008/03/24/who-s-afraid-of-the-big-bad-cast-operator.aspx#8336434</link><pubDate>Tue, 25 Mar 2008 23:02:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8336434</guid><dc:creator>Shahar</dc:creator><description>&lt;p&gt;If a button is not guaranteed to exist or if .FindControl(&amp;quot;button&amp;quot;) may return a different class than Button, then this is, in mind, a great way of using &amp;quot;as&amp;quot;.&lt;/p&gt;
&lt;p&gt;If, on the other hand, your application is guaranteed to always have a button under that name (which it doesn't), a cast is probably better.&lt;/p&gt;
</description></item><item><title>re: Who's Afraid of the big-bad cast operator?</title><link>http://blogs.msdn.com/cumgranosalis/archive/2008/03/24/who-s-afraid-of-the-big-bad-cast-operator.aspx#8337427</link><pubDate>Wed, 26 Mar 2008 11:14:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8337427</guid><dc:creator>thelever</dc:creator><description>&lt;p&gt;If you embark on a voyage that will have you do both &amp;quot;is&amp;quot; and &amp;quot;as&amp;quot;, you are most likely wasting time as &amp;quot;is&amp;quot; is basically &amp;quot;as&amp;quot; with a check for null. You are now doing that twice.&lt;/p&gt;
</description></item></channel></rss>