<?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>C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx</link><description>Hey folks, First off, I want to appologize for not having any activity on my blog for a while. I just got back from a wonderful 3 week vacation in Spain. Now that I'm back, rested and limber, here's a twisted peice of C# code which is gauranteed to turn</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#490998</link><pubDate>Thu, 10 Nov 2005 00:31:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:490998</guid><dc:creator>Hasani</dc:creator><description>class B : A&amp;lt;B&amp;gt;&lt;br&gt;&lt;br&gt;should be&lt;br&gt;&lt;br&gt;class B&amp;lt;X&amp;gt; : A&lt;br&gt;or&lt;br&gt;class B&amp;lt;X&amp;gt; : A&amp;lt;Y&amp;gt;&lt;br&gt;&lt;br&gt;I noticed, B is defined twice in the fifth line your code. Maybe that confused the compiler as it has confused me ;)</description></item><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#491000</link><pubDate>Thu, 10 Nov 2005 00:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491000</guid><dc:creator>tzagotta</dc:creator><description>This code crashes my installation of the RTM VS2005 IDE!  It crashes it if I copy/paste the code in, or if I type it in.</description></item><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#491011</link><pubDate>Thu, 10 Nov 2005 00:49:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491011</guid><dc:creator>Stuart Ballard</dc:creator><description>Easy (at least if you read and grokked the fullness of Cyrus's (I think) similar quiz a few months ago which stumped me for DAYS).&lt;br&gt;&lt;br&gt;The base class getT() returns A&amp;lt;B&amp;gt;.B (because B inherits from A&amp;lt;B&amp;gt;) while the derived class getT() returns A&amp;lt;T&amp;gt;.B (because it's taking the B from the containing scope).</description></item><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#491013</link><pubDate>Thu, 10 Nov 2005 00:51:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491013</guid><dc:creator>Stuart Ballard</dc:creator><description>Still a good puzzle though (and ok, it still took me a minute or two to re-train my brain to think inside out in the right way).&lt;br&gt;&lt;br&gt;The fix (implied but not actually stated in my previous comment) is to change the override line to read &amp;quot;public override A&amp;lt;B&amp;gt;.B getT()&amp;quot;.</description></item><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#491018</link><pubDate>Thu, 10 Nov 2005 01:02:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491018</guid><dc:creator>kfarmer</dc:creator><description>tzagotta:  The crash is probably the known infinite recursion bug in the IDE.  It's something, I believe, that the C# IDE team's already looking into a hotfix for.&lt;br&gt;</description></item><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#491025</link><pubDate>Thu, 10 Nov 2005 01:08:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491025</guid><dc:creator>Jeff Lewis</dc:creator><description>The line:&lt;br&gt;public override B getT()&lt;br&gt;&lt;br&gt;Should be:&lt;br&gt;public override T getT()</description></item><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#491040</link><pubDate>Thu, 10 Nov 2005 01:40:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491040</guid><dc:creator>geoff.appleby</dc:creator><description>Yup, crashes my IDE as well - at least, it consumes 100% cpu and I have to kill it off.&lt;br&gt;&lt;br&gt;It dies specifically when I type the &amp;quot;&amp;lt;B&amp;quot; in &amp;quot;class B : A&amp;lt;B&amp;gt;&amp;quot;. It's also the line I was suspicious of when I first read the code.&lt;br&gt;&lt;br&gt;Interestingly though, I converted that code to VB and it compiles fine, and my IDE survives :)&lt;br&gt;&lt;br&gt;Public MustInherit Class A(Of T)&lt;br&gt;&lt;br&gt;    Public MustOverride Function getT() As T&lt;br&gt;&lt;br&gt;    Class B&lt;br&gt;        Inherits A(Of B)&lt;br&gt;&lt;br&gt;        Public Overrides Function getT() As A(Of T).B&lt;br&gt;            Throw New System.Exception(&amp;quot;The method or operation is not implemented.&amp;quot;)&lt;br&gt;        End Function&lt;br&gt;    End Class&lt;br&gt;&lt;br&gt;End Class&lt;br&gt;&lt;br&gt;</description></item><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#491059</link><pubDate>Thu, 10 Nov 2005 02:54:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491059</guid><dc:creator>Ryan</dc:creator><description>Teaches me to copy code from the RSS feed without looking at the comments first.  I just lost a bit of work...&lt;br&gt;&lt;br&gt;</description></item><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#491088</link><pubDate>Thu, 10 Nov 2005 04:15:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491088</guid><dc:creator>Javier G. Lozano</dc:creator><description>I'm running VS2005 beta2 (doing some WCF development) and I got the compiler error you spoke &lt;br&gt;about Peter.  The problem is that since B is an internal class inside A&amp;lt;T&amp;gt;, in otherwords A&amp;lt;T&amp;gt;.B.&lt;br&gt;&lt;br&gt;Your method signature for the override should be public override A&amp;lt;T&amp;gt;.B getT() since that's B's &lt;br&gt;actual type name.  The code should look like this:&lt;br&gt;&lt;br&gt;&lt;br&gt;abstract class A&amp;lt;T&amp;gt;&lt;br&gt;{&lt;br&gt;  public abstract T getT();&lt;br&gt; &lt;br&gt;  class B : A&amp;lt;B&amp;gt;&lt;br&gt;  {&lt;br&gt;	public override A&amp;lt;T&amp;gt;.B getT()&lt;br&gt;	{&lt;br&gt;		throw System.Exception(&amp;quot;The method or operation is not implemented.&amp;quot;);&lt;br&gt;	}&lt;br&gt;  }&lt;br&gt;}&lt;br&gt;&lt;br&gt;If you use the code snippets feature of the IDE to create the implemenation of the override in&lt;br&gt;class B, you will get this signature:&lt;br&gt;&lt;br&gt;	public override B getT()&lt;br&gt;&lt;br&gt;Which as mentioned is wrong.  However, if you implement the class in VB.NET like Geoff did, then&lt;br&gt;the IDE gets your the right signature&lt;br&gt;&lt;br&gt;	' VB.NET Code&lt;br&gt;	public overrides Function getT() as A(Of T).B&lt;br&gt;&lt;br&gt;        // C# code&lt;br&gt;	public override A&amp;lt;T&amp;gt;.B getT()&lt;br&gt;&lt;br&gt;(This seems to be in the case in VS2005 beta2.  I'm not sure about RTM.)&lt;br&gt;&lt;br&gt;Aren't generics fun?</description></item><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#491107</link><pubDate>Thu, 10 Nov 2005 04:55:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491107</guid><dc:creator>Stuart Ballard</dc:creator><description>Hey, how come my comments giving the answer aren't showing up?</description></item><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#491127</link><pubDate>Thu, 10 Nov 2005 06:09:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491127</guid><dc:creator>Stuart Ballard</dc:creator><description>I don't know why my posts aren't showing up, but I found Cyrus's quiz earlier that allowed me to figure this one out so quickly.&lt;br&gt;&lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/cyrusn/archive/2005/08/01/446431.aspx"&gt;http://blogs.msdn.com/cyrusn/archive/2005/08/01/446431.aspx&lt;/a&gt;&lt;br&gt;&lt;br&gt;If you read my comments on that one you can see my brain gradually turning inside out until the answer made sense ;)</description></item><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#491172</link><pubDate>Thu, 10 Nov 2005 09:34:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491172</guid><dc:creator>Jeswin P.</dc:creator><description>Changing&lt;br&gt;  public override B getT()&lt;br&gt;To &lt;br&gt;  public override A&amp;lt;T&amp;gt;.B getT()&lt;br&gt;compiles properly.&lt;br&gt;&lt;br&gt;</description></item><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#491246</link><pubDate>Thu, 10 Nov 2005 14:02:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491246</guid><dc:creator>barrkel</dc:creator><description>Geoff: your code isn't identical. The return type of your overridden getT() is (in C#) A&amp;lt;T&amp;gt;.B, while the C# code Peter posted returns B from the overridden getT().&lt;br&gt;&lt;br&gt;The reason the original C# code doesn't compile is that there are two B's: &lt;br&gt;&lt;br&gt;1) A&amp;lt;B&amp;gt;.B, nested inside the A&amp;lt;B&amp;gt; that B derives from, in scope because of inheritance.&lt;br&gt;&lt;br&gt;2) The outermost B, A&amp;lt;T&amp;gt;.B, the one that is being defined, in scope because of lexical nesting.&lt;br&gt;&lt;br&gt;It appears that the inherited A&amp;lt;B&amp;gt;.B is what is being referred to when B is used nakedly, as in the example, when it should refer to the outer B, A&amp;lt;T&amp;gt;.B, which is the return-type of the overridden method.&lt;br&gt;&lt;br&gt;I'm not intimiately familiar with the precedence of scope nesting in C#: it appears that the inheritance scope of a nested class comes ahead (or is pushed on the scope stack last, if you will) of the immediate outer lexical scope. Interesting.&lt;br&gt;</description></item><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#491309</link><pubDate>Thu, 10 Nov 2005 18:10:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491309</guid><dc:creator>Stuart Ballard</dc:creator><description>Ok, I'm confused. I posted a whole bunch of comments, the first before 5pm EST yesterday giving the same correct solution that barrkel gave this morning and pointers to other relevant articles, and none of them showed up.&lt;br&gt;&lt;br&gt;I'm making this post from IE in case the blog is blocking postings from Firefox...&lt;br&gt;&lt;br&gt;What's wrong with my comments? :(</description></item><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#491544</link><pubDate>Fri, 11 Nov 2005 01:00:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491544</guid><dc:creator>geoff.appleby</dc:creator><description>Barry: Yeah, I know mine's not identical, it's also correct *laughs* &lt;br&gt;&lt;br&gt;OK, maybe not correct, but it's what the VB ide inserted for me with all it's autocomplete stuff.</description></item><item><title>re: C# Stumper: Why does this code not compile?</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#491665</link><pubDate>Fri, 11 Nov 2005 07:15:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491665</guid><dc:creator>Javier G. Lozano</dc:creator><description>I'm running VS2005 beta2 (doing some WCF development) and I got the compiler error you spoke &lt;br&gt;about Peter.  The problem is that since B is an internal class inside A&amp;lt;T&amp;gt;, in otherwords A&amp;lt;T&amp;gt;.B.&lt;br&gt;&lt;br&gt;Your method signature for the override should be public override A&amp;lt;T&amp;gt;.B getT() since that's B's &lt;br&gt;actual type name.  The code should look like this:&lt;br&gt;&lt;br&gt;&lt;br&gt;abstract class A&amp;lt;T&amp;gt;&lt;br&gt;{&lt;br&gt;  public abstract T getT();&lt;br&gt; &lt;br&gt;  class B : A&amp;lt;B&amp;gt;&lt;br&gt;  {&lt;br&gt;	public override A&amp;lt;T&amp;gt;.B getT()&lt;br&gt;	{&lt;br&gt;		throw System.Exception(&amp;quot;The method or operation is not implemented.&amp;quot;);&lt;br&gt;	}&lt;br&gt;  }&lt;br&gt;}&lt;br&gt;&lt;br&gt;If you use the code snippets feature of the IDE to create the implemenation of the override in&lt;br&gt;class B, you will get this signature:&lt;br&gt;&lt;br&gt;	public override B getT()&lt;br&gt;&lt;br&gt;Which as mentioned is wrong.  However, if you implement the class in VB.NET like Geoff did, then&lt;br&gt;the IDE gets your the right signature&lt;br&gt;&lt;br&gt;	' VB.NET Code&lt;br&gt;	public overrides Function getT() as A(Of T).B&lt;br&gt;&lt;br&gt;        // C# code&lt;br&gt;	public override A&amp;lt;T&amp;gt;.B getT()&lt;br&gt;&lt;br&gt;(This seems to be in the case in VS2005 beta2.  I'm not sure about RTM.)&lt;br&gt;&lt;br&gt;Aren't generics fun?&lt;br&gt;&lt;br&gt;</description></item><item><title> Peter Hallam s WebLog C Stumper Why does this code not compile | alternative dating</title><link>http://blogs.msdn.com/peterhal/archive/2005/11/09/490985.aspx#9768350</link><pubDate>Wed, 17 Jun 2009 10:51:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9768350</guid><dc:creator> Peter Hallam s WebLog C Stumper Why does this code not compile | alternative dating</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://topalternativedating.info/story.php?id=7787"&gt;http://topalternativedating.info/story.php?id=7787&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>