<?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>Brain Teaser #3</title><link>http://blogs.msdn.com/ben/archive/2008/05/07/brain-teaser-3.aspx</link><description>This brain teaser also comes courtesy of Simeon Cran. What will the following code output? When you think you know, copy the code into Foo.cs and run &amp;#8220;csc Foo.cs&amp;#8221; and then run &amp;#8220;Foo.exe&amp;#8221;. Did it output what you expected? The brain</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Brain Teaser #3</title><link>http://blogs.msdn.com/ben/archive/2008/05/07/brain-teaser-3.aspx#8467477</link><pubDate>Thu, 08 May 2008 01:15:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8467477</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;Make your struct to a class and it should work as expected. Structs are copy by value and not reference.&lt;/p&gt;
</description></item><item><title>re: Brain Teaser #3</title><link>http://blogs.msdn.com/ben/archive/2008/05/07/brain-teaser-3.aspx#8467580</link><pubDate>Thu, 08 May 2008 01:36:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8467580</guid><dc:creator>benmartens</dc:creator><description>&lt;p&gt;Thanks for the reply. You are correct that a class does not exhibit this behavior, but the brain teaser here is WHY. You mention a copy, but where/why does that copy happen?&lt;/p&gt;
&lt;p&gt;Answer will be posted on Tuesday.&lt;/p&gt;
</description></item><item><title>re: Brain Teaser #3</title><link>http://blogs.msdn.com/ben/archive/2008/05/07/brain-teaser-3.aspx#8467666</link><pubDate>Thu, 08 May 2008 01:53:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8467666</guid><dc:creator>Ryan Heaney</dc:creator><description>&lt;p&gt;The using statements seem to do everything on local copies of the structs, so it would probably make their copies right at the start of the using block (but after the new). The copies are disposed and not the outside declared foo. &lt;/p&gt;
&lt;p&gt;In the second example, it actually seems as if calling the dispose explicitly is calling it on the original instance made by the second new, but then the using's call to dispose calls it on its local copy of foo.&lt;/p&gt;
&lt;p&gt;Looking at the IL code for this, it does look like stack space for three instances of Foo are allocated and foo2 and foo3 are using within the first and second using's respectively.&lt;/p&gt;
&lt;p&gt;Seems like structs and usings are quite unpredictable when it comes to situations like this.&lt;/p&gt;
</description></item><item><title>C# Brain Teaser #3</title><link>http://blogs.msdn.com/ben/archive/2008/05/07/brain-teaser-3.aspx#8467741</link><pubDate>Thu, 08 May 2008 02:12:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8467741</guid><dc:creator>DotNetKicks.com</dc:creator><description>&lt;p&gt;You've been kicked (a good thing) - Trackback from DotNetKicks.com&lt;/p&gt;
</description></item><item><title>re: Brain Teaser #3</title><link>http://blogs.msdn.com/ben/archive/2008/05/07/brain-teaser-3.aspx#8472404</link><pubDate>Thu, 08 May 2008 18:25:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8472404</guid><dc:creator>Unai</dc:creator><description>&lt;p&gt;In the adcquisition phase of the using de disposable element is a new Structure Foo, the call to Dispose Method is not in foo.&lt;/p&gt;
&lt;p&gt;In the second using, you use de foo in using try phase and set dispose to TRUE..&lt;/p&gt;
&lt;p&gt;This code is similar to&lt;/p&gt;
&lt;p&gt;Foo foo;&lt;/p&gt;
&lt;p&gt;Foo newfoo = new Foo();&lt;/p&gt;
&lt;p&gt;using(foo = newfoo) &lt;/p&gt;
&lt;p&gt;{}&lt;/p&gt;
&lt;p&gt;//Call to newfoo.Dispose()&lt;/p&gt;
&lt;p&gt;using(foo = new Foo())&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; foo.Dispose();//set disposed = true&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Unai&lt;/p&gt;
</description></item><item><title>re: Brain Teaser #3</title><link>http://blogs.msdn.com/ben/archive/2008/05/07/brain-teaser-3.aspx#8476861</link><pubDate>Fri, 09 May 2008 06:28:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8476861</guid><dc:creator>Raj Kaimal</dc:creator><description>&lt;p&gt;To call IDisposable, foo is boxed resulting in the heap instance changing its &amp;quot;disposed&amp;quot; value. The value on the stack is not affected. &lt;/p&gt;
&lt;p&gt;Similar to this:&lt;/p&gt;
&lt;p&gt;int i = 5;&lt;/p&gt;
&lt;p&gt;object o = i;&lt;/p&gt;
&lt;p&gt;o = ((int)o) + 1;&lt;/p&gt;
&lt;p&gt;Console.WriteLine(o.ToString());&lt;/p&gt;
&lt;p&gt;Console.WriteLine(i.ToString());&lt;/p&gt;
&lt;p&gt;Raj&lt;/p&gt;
</description></item><item><title>re: Brain Teaser #3</title><link>http://blogs.msdn.com/ben/archive/2008/05/07/brain-teaser-3.aspx#8480950</link><pubDate>Fri, 09 May 2008 19:56:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8480950</guid><dc:creator>Chris</dc:creator><description>&lt;p&gt;Raj, your explanation is great, but it does not explain why the last time the value of foo.disposed is displayed it is 'true'. If the boxed instances are the only instances affected, then why does the non-boxed foo change?&lt;/p&gt;
</description></item><item><title>re: Brain Teaser #3</title><link>http://blogs.msdn.com/ben/archive/2008/05/07/brain-teaser-3.aspx#8480990</link><pubDate>Fri, 09 May 2008 20:04:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8480990</guid><dc:creator>benmartens</dc:creator><description>&lt;p&gt;Boxing is not the answer. If it was boxing, this code would print “Hits: 2” but it doesn’t:&lt;/p&gt;
&lt;p&gt;using System;&lt;/p&gt;
&lt;p&gt;struct Foo : IDisposable&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;int hitCounter;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public void HitMe()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;hitCounter++;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public void Dispose()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;Hits: &amp;quot; + hitCounter);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;static void Main(string[] args)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Foo foo;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;using (foo = new Foo())&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foo.HitMe();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foo.HitMe();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>re: Brain Teaser #3</title><link>http://blogs.msdn.com/ben/archive/2008/05/07/brain-teaser-3.aspx#8482279</link><pubDate>Sat, 10 May 2008 00:59:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8482279</guid><dc:creator>Raj Kaimal</dc:creator><description>&lt;p&gt;Chris,&lt;/p&gt;
&lt;p&gt;My intial thinking was that since the using statement&lt;/p&gt;
&lt;p&gt;Foo foo;&lt;/p&gt;
&lt;p&gt;using (foo = new Foo())&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;translates to the following:&lt;/p&gt;
&lt;p&gt;Foo foo = new Foo();&lt;/p&gt;
&lt;p&gt;try&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	foo.Dispose();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;finally {&lt;/p&gt;
&lt;p&gt;	((IDisposable)foo).Dispose();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;I thought that foo would get boxed. But that is apparently not the case as Ben posted above. The complier will call Dispose directly without boxing it.&lt;/p&gt;
&lt;p&gt;My thinking now, after reading the C# specs, is that, because of a resource aqcuisition, the foo in &lt;/p&gt;
&lt;p&gt;using (foo = new Foo()) is readonly and is not accessible to the embedded statement. Therefore the compiler makes an extra copy of foo on the stack before the embeded statement is first hit.&lt;/p&gt;
</description></item><item><title>Ultram.</title><link>http://blogs.msdn.com/ben/archive/2008/05/07/brain-teaser-3.aspx#8763131</link><pubDate>Tue, 22 Jul 2008 08:19:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8763131</guid><dc:creator>Ultram.</dc:creator><description>&lt;p&gt;Ultram. Can you snort ultram. Ultram side effects.&lt;/p&gt;
</description></item></channel></rss>