<?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>Fabulous Adventures In Coding : Psychic Debugging</title><link>http://blogs.msdn.com/ericlippert/archive/tags/Psychic+Debugging/default.aspx</link><description>Tags: Psychic Debugging</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Psychic Debugging, Part Two</title><link>http://blogs.msdn.com/ericlippert/archive/2007/09/06/psychic-debugging-part-two.aspx</link><pubDate>Thu, 06 Sep 2007 18:31:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4789522</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/4789522.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=4789522</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;A number of readers have the &lt;A class="" href="http://blogs.msdn.com/ericlippert/archive/2004/08/20/i-have-a-mysterious-fifth-sense.aspx" mce_href="http://blogs.msdn.com/ericlippert/archive/2004/08/20/i-have-a-mysterious-fifth-sense.aspx"&gt;mysterious fifth sense&lt;/A&gt; which gives them the ability to deduce that the &lt;SPAN class=code&gt;GetBars&lt;/SPAN&gt; method from &lt;A class="" href="http://blogs.msdn.com/ericlippert/archive/2007/09/05/psychic-debugging-part-one.aspx" mce_href="http://blogs.msdn.com/ericlippert/archive/2007/09/05/psychic-debugging-part-one.aspx"&gt;yesterday's post&lt;/A&gt; contains a &lt;SPAN class=code&gt;yield return&lt;/SPAN&gt; and is therefore an iterator. Remember, as the standard states (in section 10.14.4):&lt;/P&gt;&lt;SPAN class=spec&gt;
&lt;P&gt;[...] execution of the code in the iterator block occurs when the enumerator object's MoveNext method is invoked.&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;Since the test program does not invoke &lt;SPAN class=code&gt;MoveNext&lt;/SPAN&gt;, the check for &lt;SPAN class=code&gt;null&lt;/SPAN&gt; is never executed, and therefore the exception is never thrown.&lt;/P&gt;
&lt;P&gt;Since most of the interesting new sequence operators available in C# 3.0 are implemented with iterators it probably will be increasingly important for developers to understand a bit more about how iterators work behind the scenes. I may do some blog posts on that over the next little while.&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4789522" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Psychic+Debugging/default.aspx">Psychic Debugging</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Puzzles/default.aspx">Puzzles</category></item><item><title>Psychic Debugging, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2007/09/05/psychic-debugging-part-one.aspx</link><pubDate>Wed, 05 Sep 2007 20:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4766755</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/4766755.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=4766755</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Here is a compiler bug report I got the other day. The user is trying to write a unit test for a method which takes a &lt;SPAN class=code&gt;Foo&lt;/SPAN&gt; and returns a collection of &lt;SPAN class=code&gt;Bar&lt;/SPAN&gt;s. The test is supposed to confirm that &lt;SPAN class=code&gt;GetBars&lt;/SPAN&gt; throws an exception if the argument is &lt;SPAN class=code&gt;null&lt;/SPAN&gt;. The test was failing with “got no exception”. The user was wondering if somehow the compiler had optimized away the call.&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static void Test()&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; bool gotException = false;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IEnumerable&amp;lt;Bar&amp;gt; bars = Foo.GetBars(null);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; catch (ArgumentNullException ex) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine("SUCCESS: Got expected exception");&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gotException = true;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; catch (Exception ex) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine("FAILURE: Got unexpected exception");&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gotException = true;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finally {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!gotException)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine("FAILURE: Got no exception");&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;Several people (including myself and &lt;A class="" href="http://blogs.msdn.com/cyrusn/" mce_href="http://blogs.msdn.com/cyrusn/"&gt;Cyrus&lt;/A&gt;) psychically debugged this one independently. The user did not include the source code of &lt;SPAN class=code&gt;GetBars&lt;/SPAN&gt;, which is what necessitated the use of our psychic powers. Because I am a nice guy, I will give you the beginning:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static public IEnumerable&amp;lt;Bar&amp;gt; GetBars(Foo foo)&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (foo == null) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new ArgumentNullException("foo");&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;&lt;STRONG&gt;Our psychic powers correctly told us that the behaviour of the test program is correct and expected.&lt;/STRONG&gt; What do your psychic powers tell you about the rest of the implementation of &lt;SPAN class=code&gt;GetBars&lt;/SPAN&gt;? What is going on here?&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4766755" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Psychic+Debugging/default.aspx">Psychic Debugging</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Puzzles/default.aspx">Puzzles</category></item></channel></rss>