<?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>An "is" operator puzzle, part two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/08/27/an-quot-is-quot-operator-puzzle-part-two.aspx</link><description>As I said last time , that was a pretty easy puzzle: either FooBar, or the type of local variable x, can be a type parameter. That is: void M&amp;lt;FooBar&amp;gt;() { int x = 0; bool b = x is FooBar; // legal, true if FooBar is int. FooBar fb = (FooBar)x; /</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: An "is" operator puzzle, part two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/08/27/an-quot-is-quot-operator-puzzle-part-two.aspx#10347912</link><pubDate>Mon, 10 Sep 2012 19:22:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10347912</guid><dc:creator>Robert C</dc:creator><description>&lt;p&gt;Interesting topic Eric. You can get it to work if you use &amp;#39;as&amp;#39; instead, but you also need the change the struct to a class. This is certainly bad coding for standards, but to show a point, this works: &lt;/p&gt;
&lt;p&gt;class FooBar { /* ... */ }&lt;/p&gt;
&lt;p&gt;void M&amp;lt;X&amp;gt;()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;X x = default(X);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;bool b = x is FooBar; // legal, true if X is FooBar&lt;/p&gt;
&lt;p&gt; &amp;nbsp;FooBar fb = x as FooBar; // legal, may return null&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10347912" width="1" height="1"&gt;</description></item><item><title>re: An "is" operator puzzle, part two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/08/27/an-quot-is-quot-operator-puzzle-part-two.aspx#10346117</link><pubDate>Tue, 04 Sep 2012 09:40:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10346117</guid><dc:creator>Bhavesh</dc:creator><description>&lt;p&gt;I went a little simpler &amp;amp; just derived FooBar2 from FooBar :).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10346117" width="1" height="1"&gt;</description></item><item><title>re: An "is" operator puzzle, part two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/08/27/an-quot-is-quot-operator-puzzle-part-two.aspx#10345944</link><pubDate>Mon, 03 Sep 2012 14:53:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10345944</guid><dc:creator>h.v.dijk</dc:creator><description>&lt;p&gt;@Anthony: I&amp;#39;d say &amp;quot;can be cast without causing an exception to be thrown&amp;quot; is two requirements. 1) &amp;quot;Can be cast&amp;quot; -- the cast must be accepted by the compiler. 2) &amp;quot;without causing an exception to be thrown&amp;quot; -- the cast must be accepted by the .NET runtime.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10345944" width="1" height="1"&gt;</description></item><item><title>re: An "is" operator puzzle, part two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/08/27/an-quot-is-quot-operator-puzzle-part-two.aspx#10345711</link><pubDate>Sun, 02 Sep 2012 11:20:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10345711</guid><dc:creator>W</dc:creator><description>&lt;p&gt;There are a few other fun corner cases where `is` behaves strangely.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;var a=new int[0];&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;a is uint[] =&amp;gt; false&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;(object)a is uint[] =&amp;gt; true&lt;/p&gt;
&lt;p&gt;The underlying issue is that the CLR allows this conversion, but C# doesn&amp;#39;t.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10345711" width="1" height="1"&gt;</description></item><item><title>re: An "is" operator puzzle, part two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/08/27/an-quot-is-quot-operator-puzzle-part-two.aspx#10345075</link><pubDate>Thu, 30 Aug 2012 16:51:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10345075</guid><dc:creator>Anthony W Pegram</dc:creator><description>&lt;p&gt;@Ben, if I were a lawyer I would argue that something being compile-time illegal does not contradict a statement about something not producing a runtime exception.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10345075" width="1" height="1"&gt;</description></item><item><title>re: An "is" operator puzzle, part two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/08/27/an-quot-is-quot-operator-puzzle-part-two.aspx#10344962</link><pubDate>Thu, 30 Aug 2012 11:15:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10344962</guid><dc:creator>Kabwla Two-Lips</dc:creator><description>&lt;p&gt;Gabe: true.&lt;/p&gt;
&lt;p&gt;Yet, I seldom produce code where casting needed, casting Value-Types is even more rare.&lt;/p&gt;
&lt;p&gt;I prefer to use generics.&lt;/p&gt;
&lt;p&gt;Probably I&amp;#39;m just lucky with the kinds of projects I am working on and/or what libraries I have to integrate with.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10344962" width="1" height="1"&gt;</description></item><item><title>re: An "is" operator puzzle, part two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/08/27/an-quot-is-quot-operator-puzzle-part-two.aspx#10344715</link><pubDate>Wed, 29 Aug 2012 17:20:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10344715</guid><dc:creator>MgSm88</dc:creator><description>&lt;p&gt;Completely off topic- Eric will you be making an appearance at the BUILD event in October? It would be awesome if you make an appearance at one of the events.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10344715" width="1" height="1"&gt;</description></item><item><title>re: An "is" operator puzzle, part two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/08/27/an-quot-is-quot-operator-puzzle-part-two.aspx#10344324</link><pubDate>Tue, 28 Aug 2012 18:57:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10344324</guid><dc:creator>CM</dc:creator><description>&lt;p&gt;I see no contradiction. Whether a cast is a legal C# expression and whether performing a cast would cause an exception are two very different questions.&lt;/p&gt;
&lt;p&gt;If object X, which exists when the program is run, may be cast to type T without causing an exception in the running program, it does not follow that the compiler must accept any particular piece of code where an identifier that will reference object X at run time is used in a cast expression to a type that will be T at run time.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10344324" width="1" height="1"&gt;</description></item><item><title>re: An "is" operator puzzle, part two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/08/27/an-quot-is-quot-operator-puzzle-part-two.aspx#10344293</link><pubDate>Tue, 28 Aug 2012 17:56:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10344293</guid><dc:creator>Ben Voigt</dc:creator><description>&lt;p&gt;Don&amp;#39;t know where my comment to the last post got eaten, but your statement here &amp;quot;an `is` expression can result in true even if the corresponding cast would be illegal&amp;quot; contradicts the documentation &amp;quot;An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown.&amp;quot; &amp;nbsp;&lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/en-us/library/scekt9xw.aspx"&gt;msdn.microsoft.com/.../scekt9xw.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Apparently it&amp;#39;s a documentation bug.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10344293" width="1" height="1"&gt;</description></item><item><title>re: An "is" operator puzzle, part two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/08/27/an-quot-is-quot-operator-puzzle-part-two.aspx#10344241</link><pubDate>Tue, 28 Aug 2012 16:03:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10344241</guid><dc:creator>Gabe</dc:creator><description>&lt;p&gt;Kabwla: Your code doesn&amp;#39;t work when MyType is a value type. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10344241" width="1" height="1"&gt;</description></item></channel></rss>