<?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>jaredpar's WebLog : Orcas</title><link>http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx</link><description>Tags: Orcas</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Type Inference and IEnumerable</title><link>http://blogs.msdn.com/jaredpar/archive/2007/11/26/type-inference-and-ienumerable.aspx</link><pubDate>Tue, 27 Nov 2007 01:32:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6536165</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/6536165.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=6536165</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=6536165</wfw:comment><description>&lt;p&gt;This is somewhat of a follow up on a previous &lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/10/04/ienumerable-and-ienumerable-of-t.aspx"&gt;post&lt;/a&gt; I did on the difference between &lt;a href="http://msdn2.microsoft.com/en-us/library/9eekhta0.aspx"&gt;IEnumerable(Of T)&lt;/a&gt; and the &lt;a href="http://msdn2.microsoft.com/en-us/library/9eekhta0.aspx"&gt;IEnumerable&lt;/a&gt; interfaces.&amp;nbsp; &lt;/p&gt; &lt;p&gt;I've seen several people type in the following code and wonder if there was a fundamental bug in the type inference code.&lt;/p&gt;&lt;pre class="code"&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Sub&lt;/span&gt; Form1_Load(&lt;span style="color: rgb(0,0,255)"&gt;ByVal&lt;/span&gt; sender &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; System.Object, &lt;span style="color: rgb(0,0,255)"&gt;ByVal&lt;/span&gt; e &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color: rgb(0,0,255)"&gt;Handles&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;MyBase&lt;/span&gt;.Load
        &lt;span style="color: rgb(0,0,255)"&gt;For&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Each&lt;/span&gt; cur &lt;span style="color: rgb(0,0,255)"&gt;In&lt;/span&gt; Controls
            cur.Text = &lt;span style="color: rgb(163,21,21)"&gt;"A Value"
&lt;/span&gt;        &lt;span style="color: rgb(0,0,255)"&gt;Next
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Sub&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;This code will produce an error stating that "Text" is not a member of object.&amp;nbsp; Users expected type inference to type the variable "cur" as Control.&amp;nbsp; Unfortunately this is "By Design".&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Much of the original .Net Framework was written before the CLR implemented support for generics.&amp;nbsp; As a result all of the collection classes were loosely typed to Object by implementing &lt;a href="http://msdn2.microsoft.com/en-us/library/9eekhta0.aspx"&gt;IEnumerable&lt;/a&gt;.&amp;nbsp; So in this case type inference will correctly type this as Object.&lt;/p&gt;
&lt;p&gt;There are 2 ways to fix this problem. &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Explicitly type the For Each variable to be the actual type of objects in the collection&lt;/li&gt;
&lt;li&gt;Use a Shim to change the type of the collection. (see &lt;a title="http://blogs.msdn.com/jaredpar/archive/2007/10/04/ienumerable-and-ienumerable-of-t.aspx" href="http://blogs.msdn.com/jaredpar/archive/2007/10/04/ienumerable-and-ienumerable-of-t.aspx"&gt;http://blogs.msdn.com/jaredpar/archive/2007/10/04/ienumerable-and-ienumerable-of-t.aspx&lt;/a&gt;)&lt;/li&gt;&lt;/ol&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6536165" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Generics/default.aspx">Generics</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Type+Inference/default.aspx">Type Inference</category></item><item><title>VS2008 Ships</title><link>http://blogs.msdn.com/jaredpar/archive/2007/11/19/vs2008-ships.aspx</link><pubDate>Tue, 20 Nov 2007 06:41:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6419429</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/6419429.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=6419429</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=6419429</wfw:comment><description>&lt;p&gt;Orcas has shipped :)&lt;/p&gt;  &lt;p&gt;&lt;a title="http://msdn2.microsoft.com/en-us/vstudio/default.aspx" href="http://msdn2.microsoft.com/en-us/vstudio/default.aspx"&gt;http://msdn2.microsoft.com/en-us/vstudio/default.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6419429" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx">Orcas</category></item><item><title>Lambda Unexpected Behavior</title><link>http://blogs.msdn.com/jaredpar/archive/2007/11/19/lambda-unexpected-behavior.aspx</link><pubDate>Tue, 20 Nov 2007 06:38:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6419369</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/6419369.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=6419369</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=6419369</wfw:comment><description>&lt;p&gt;One item you strive to avoid when you design and implement a feature is unexpected behavior.&amp;#160; Unfortunately there is one case we couldn't avoid with Lambda's in VB9.&amp;#160; I just ran into the this problem when coding up a handler.&amp;#160; I wanted to disable a button when the text of particular TextBox was empty.&amp;#160; I wrote the following code to handle the situation.&lt;/p&gt;  &lt;pre&gt;&lt;span style="color: blue"&gt;AddHandler&lt;/span&gt; c.TextChanged, &lt;span style="color: blue"&gt;Function&lt;/span&gt;() okButton.Enabled = (&lt;span style="color: maroon"&gt;0&lt;/span&gt; &amp;lt;&amp;gt; c.Text.Length)&lt;/pre&gt;

&lt;p&gt;This doesn't quite do what I intended.&amp;#160; This instead will simply compare the two values.&amp;#160; &lt;/p&gt;

&lt;p&gt;In VB9 Lambda Expressions are always an expression.&amp;#160; In version 9 of VB, there is no concept of an assignment as an expression.&amp;#160; There is only a statement version.&amp;#160; As a result this doesn't do anything useful.&amp;#160; &lt;/p&gt;

&lt;p&gt;This has tripped up a few people along the way.&amp;#160; It's an unfortunate side effect of only supporting expression lambdas.&amp;#160; &lt;/p&gt;

&lt;p&gt;I was able to work around this by defining a function which did what I intended.&amp;#160; I called this function in the lambda expression and the problem was solved. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6419369" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Lambda/default.aspx">Lambda</category></item><item><title>Extension Methods without 3.5 Framework</title><link>http://blogs.msdn.com/jaredpar/archive/2007/11/16/extension-methods-without-3-5-framework.aspx</link><pubDate>Fri, 16 Nov 2007 23:37:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6316111</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>10</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/6316111.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=6316111</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=6316111</wfw:comment><description>&lt;p&gt;For a time I've been avoiding extension methods.&amp;nbsp; Not because I'm opposed to using them but because of the 3.5 Framework.&amp;nbsp; &lt;/p&gt; &lt;p&gt;A lot of the tools I own are designed to be very light weight tools that only require the user to have 2.0 installed on their machine.&amp;nbsp; I find that the easier that tools are to install, the more likely people are to use them.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Extension methods require the ExtensionAttribute be available.&amp;nbsp; Since the attribute is declared in a 3.5 Framework assembly it's not possible to use extension methods without the 3.5 framework.&amp;nbsp; At least, that's what I thought up until I read an recent MSDN article.&lt;/p&gt; &lt;p&gt;You can simply define the ExtensionAttribute in your assembly and extension methods will start working.&amp;nbsp; No references to the 3.5 framework required.&amp;nbsp; It's a lightweight solution that adds the full power of extension methods to your program.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;Namespace&lt;/span&gt; System.Runtime.CompilerServices
    &lt;span style="color: rgb(0,0,255)"&gt;Class&lt;/span&gt; ExtensionAttribute
        &lt;span style="color: rgb(0,0,255)"&gt;Inherits&lt;/span&gt; Attribute
    &lt;span style="color: rgb(0,0,255)"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Class
End&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Namespace&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6316111" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Extension+Methods/default.aspx">Extension Methods</category></item><item><title>Singleton Pattern</title><link>http://blogs.msdn.com/jaredpar/archive/2007/09/04/singleton-pattern.aspx</link><pubDate>Tue, 04 Sep 2007 20:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4744922</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/4744922.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=4744922</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=4744922</wfw:comment><description>&lt;P&gt;Quite awhile back I posted about how to create a re-usable singleton pattern in .Net.&amp;nbsp; Link is &lt;A href="http://blogs.msdn.com/jaredpar/archive/2004/11/24/269133.aspx" mce_href="http://blogs.msdn.com/jaredpar/archive/2004/11/24/269133.aspx"&gt;here&lt;/A&gt;.&amp;nbsp; A bit of time has passed and I've altered the pattern a bit.&amp;nbsp; The reasons for the change are some new type inference patterns and FxCop cleanliness.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;The first pattern I introduced had a couple of FxCop violations.&amp;nbsp; Namely Microsoft.Design CA1000 - Do not declare static members on generic types.&amp;nbsp; The logic here being that static methods don't have any type inference benefits as you must explicitly add the type into the name of the type you were using (in this case Singleton).&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Secondly because T was at a class level rather than a method level I couldn't have granualar methods which had differing set of constraints.&amp;nbsp; The result was a pattern that was not always easy to write out.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;The new pattern takes care of both of these problems.&amp;nbsp; It has two methods.&amp;nbsp; One of which can be satisfied with a trivial lambda expression.&amp;nbsp; The other can easily be used for classes that satisfy the generic constraint new() with no additional lambda.&lt;/P&gt;&lt;PRE class=code&gt;    &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;c1
&lt;/SPAN&gt;    {
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;c1&lt;/SPAN&gt; Instance1 
        {
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Singleton&lt;/SPAN&gt;.GetInstance(() =&amp;gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;c1&lt;/SPAN&gt;());}
        }

        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;c1&lt;/SPAN&gt; Instance2
        {
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Singleton&lt;/SPAN&gt;.GetInstance&amp;lt;&lt;SPAN style="COLOR: rgb(43,145,175)"&gt;c1&lt;/SPAN&gt;&amp;gt;(); }
        }

        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; c1()
        {

        }
        
    }&lt;/PRE&gt;
&lt;P&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;Below is the new singleton pattern.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;    public&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;delegate&lt;/SPAN&gt; T &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Operation&lt;/SPAN&gt;&amp;lt;T&amp;gt;();&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;PRE class=code&gt;    &lt;SPAN style="COLOR: rgb(128,128,128)"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;&amp;lt;summary&amp;gt;
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: rgb(128,128,128)"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; Used for classes that are single instances per appdomain
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: rgb(128,128,128)"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;&amp;lt;/summary&amp;gt;
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Singleton
&lt;/SPAN&gt;    {
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Storage&lt;/SPAN&gt;&amp;lt;T&amp;gt;
        {
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;internal&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;static&lt;/SPAN&gt; T s_instance;
        }

        [&lt;SPAN style="COLOR: rgb(43,145,175)"&gt;SuppressMessage&lt;/SPAN&gt;(&lt;SPAN style="COLOR: rgb(163,21,21)"&gt;"Microsoft.Reliability"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: rgb(163,21,21)"&gt;"CA2002"&lt;/SPAN&gt;)]
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;static&lt;/SPAN&gt; T GetInstance&amp;lt;T&amp;gt;(&lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Operation&lt;/SPAN&gt;&amp;lt;T&amp;gt; op)
        {
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Storage&lt;/SPAN&gt;&amp;lt;T&amp;gt;.s_instance == &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;)
            {
                &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;lock&lt;/SPAN&gt; (&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Storage&lt;/SPAN&gt;&amp;lt;T&amp;gt;))
                {
                    &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Storage&lt;/SPAN&gt;&amp;lt;T&amp;gt;.s_instance == &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;)
                    {
                        T temp = op();
                        System.Threading.&lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Thread&lt;/SPAN&gt;.MemoryBarrier();
                        &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Storage&lt;/SPAN&gt;&amp;lt;T&amp;gt;.s_instance = temp;
                    }
                }
            }
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Storage&lt;/SPAN&gt;&amp;lt;T&amp;gt;.s_instance;
        }

        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;static&lt;/SPAN&gt; T GetInstance&amp;lt;T&amp;gt;()
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;where&lt;/SPAN&gt; T : &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt;()
        {
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt; GetInstance(() =&amp;gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt; T());
        }
    }

&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;    #endregion
&lt;/SPAN&gt;}
&lt;/PRE&gt;
&lt;P&gt;Edit: Originally forgot to add the signature for Operation&amp;lt;T&amp;gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4744922" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Generics/default.aspx">Generics</category></item><item><title>C# Type inference change for Orcas</title><link>http://blogs.msdn.com/jaredpar/archive/2007/08/07/c-type-inference-change-for-orcas.aspx</link><pubDate>Tue, 07 Aug 2007 19:06:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4277836</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/4277836.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=4277836</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=4277836</wfw:comment><description>&lt;p&gt;While playing around with a batch of Orcas code recently I found a welcome change to the C# type inference rules for Orcas.&amp;nbsp; The return type of a generic delegate can now be inferred from the actual return values.&amp;nbsp; Here is some sample code demonstrating the problem.&amp;nbsp; &lt;/p&gt;&lt;pre&gt;    &lt;span style="color: blue"&gt;class&lt;/span&gt; Program
    {
        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;delegate&lt;/span&gt; T Operation&amp;lt;T&amp;gt;();

        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; M&amp;lt;T&amp;gt;(Operation&amp;lt;T&amp;gt; del)
        {

        }

        &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)
        {
            M(&lt;span style="color: blue"&gt;delegate&lt;/span&gt; { &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: maroon"&gt;42&lt;/span&gt;; });
        }
    }
&lt;/pre&gt;
&lt;p&gt;In Whidbey the above code will fail with a compiler error.&lt;/p&gt;
&lt;p&gt;error CS0411: The type arguments for method 'ConsoleApplication34.Program.M&amp;lt;T&amp;gt;(ConsoleApplication34.Program.Operation&amp;lt;T&amp;gt;)' cannot be inferred from the usage. Try specifying the type arguments explicitly.&lt;/p&gt;
&lt;p&gt;This is really frustrating in Whidbey because the you have to modify the call to include the actual type specifier.&amp;nbsp; &lt;/p&gt;&lt;pre&gt;            M&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(&lt;span style="color: blue"&gt;delegate&lt;/span&gt; { &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: maroon"&gt;42&lt;/span&gt;; });
&lt;/pre&gt;
&lt;p&gt;However this code now works without modification in Orcas.&amp;nbsp; This is a welcome and extremely useful change.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;And yes, the same inference is possible with VB in Orcas.&amp;nbsp; In Whidbey this was not an issue because VB did not support lambda expressions.&amp;nbsp; &lt;/p&gt;&lt;pre&gt;&lt;span style="color: blue"&gt;Module&lt;/span&gt; Module1

    &lt;span style="color: blue"&gt;Public&lt;/span&gt; &lt;span style="color: blue"&gt;Delegate&lt;/span&gt; &lt;span style="color: blue"&gt;Function&lt;/span&gt; Operation(Of T)() &lt;span style="color: blue"&gt;As&lt;/span&gt; T

    &lt;span style="color: blue"&gt;Public&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt; M(Of T)(&lt;span style="color: blue"&gt;ByVal&lt;/span&gt; del &lt;span style="color: blue"&gt;As&lt;/span&gt; Operation(Of T))

    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;

    &lt;span style="color: blue"&gt;Sub&lt;/span&gt; Main()
        M(&lt;span style="color: blue"&gt;Function&lt;/span&gt;() &lt;span style="color: maroon"&gt;1&lt;/span&gt;)
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;

&lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Module&lt;/span&gt;&lt;/pre&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4277836" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>Closures in VB Part 6: Limitations</title><link>http://blogs.msdn.com/jaredpar/archive/2007/08/06/closures-in-vb-part-6-limitations.aspx</link><pubDate>Tue, 07 Aug 2007 01:36:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4267111</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/4267111.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=4267111</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=4267111</wfw:comment><description>&lt;p&gt;For previous articles in this series please see ...&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/04/27/closures-in-vb-part-1.aspx"&gt;Part 1: Introduction&lt;/a&gt;  &lt;li&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/05/03/closures-in-vb-part-2-method-calls.aspx"&gt;Part 2: Method Calls&lt;/a&gt;  &lt;li&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/05/25/closures-in-vb-part-3-scope.aspx"&gt;Part 3: Scope&lt;/a&gt;  &lt;li&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/06/15/closures-in-vb-part-4-variable-lifetime.aspx"&gt;Part 4: Variable Lifetime&lt;/a&gt;  &lt;li&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/07/26/closures-in-vb-part-5-looping.aspx"&gt;Part 5: Looping&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;As powerful as closures are in the language they do have a few limitations.&amp;nbsp; We worked hard in Orcas to put as few limitations in Orcas as possible.&amp;nbsp; Below are the current limitations and some insight into why they exist this way.&amp;nbsp; &lt;/p&gt; &lt;p&gt;1. Cannot use "ByRef" parameters in a closure&lt;/p&gt; &lt;p&gt;Example:&lt;/p&gt;&lt;pre&gt;    &lt;span style="color: blue"&gt;Sub&lt;/span&gt; LiftAByRef(&lt;span style="color: blue"&gt;ByRef&lt;/span&gt; x &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;)
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; f = &lt;span style="color: blue"&gt;Function&lt;/span&gt;() x
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Message: error BC36639: 'ByRef' parameter 'x' cannot be used in a lambda expression.&lt;/p&gt;
&lt;p&gt;The problem here is the expectation surrounding x.&amp;nbsp; Any change in the value of "x" inside the method "LiftAByRef" should be reflected in the calling function.&amp;nbsp;&amp;nbsp; Normally for any lifted parameter we add a new field inside the closure and all read/writes are redirected into that value.&amp;nbsp; For "ByRef" parameters we would additionally have to ensure that all writes are make to the parameter.&amp;nbsp; Even in the presence of an exception.&amp;nbsp; Not a trivial task.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;2. Cannot use "Me" in a closure created inside a structure.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: blue"&gt;Structure&lt;/span&gt; S1
    &lt;span style="color: blue"&gt;Public&lt;/span&gt; F1 &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;

    &lt;span style="color: blue"&gt;Public&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt; M1()
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; f = &lt;span style="color: blue"&gt;Function&lt;/span&gt;() F1
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;
&lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Structure&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Message: error BC36638: Instance members and 'Me' cannot be used within a lambda expression in structures&lt;/p&gt;
&lt;p&gt;Closures capture values by reference.&amp;nbsp; It's not possible to capture the "Me" of a structure by reference in VB.&amp;nbsp; The only other option is to capture them by value.&amp;nbsp; If we did that then all changes to members of a structure inside a lambda would not affect the structure in which they were created; merely the value copy.&amp;nbsp; This is very different behavior from every other place that closures are used.&amp;nbsp; To avoid confusing behavior this is not a legal operation.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;3. Cannot use a Restricted Type in a closure&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;&lt;pre&gt;    &lt;span style="color: blue"&gt;Sub&lt;/span&gt; LiftRestrictedType()
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; x &lt;span style="color: blue"&gt;As&lt;/span&gt; ArgIterator = &lt;span style="color: blue"&gt;Nothing&lt;/span&gt;
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; f = &lt;span style="color: blue"&gt;Function&lt;/span&gt;() x.GetNextArgType().GetModuleHandle()
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Message: error BC36640: Instance of restricted type 'System.ArgIterator' cannot be used in a lambda expression.&lt;/p&gt;
&lt;p&gt;This hopefully will not affect many users.&amp;nbsp; There are several types in the CLR that are considered &lt;em&gt;restricted&lt;/em&gt; because they have special semantics.&amp;nbsp; Typically they are special cased by the CLR and as such we can't use them in a closure.&amp;nbsp; Several of these cannot be used in VB at all.&amp;nbsp; They are ...&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;System.TypedReference 
&lt;li&gt;System.ArgIterator 
&lt;li&gt;System.RuntimeArgumentHandle&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;4. Cannot Goto&amp;nbsp;into scope that contains a closure &lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;&lt;pre&gt;    &lt;span style="color: blue"&gt;Sub&lt;/span&gt; BadGoto()
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; x = &lt;span style="color: maroon"&gt;0&lt;/span&gt;

        &lt;span style="color: blue"&gt;GoTo&lt;/span&gt; Label1
        &lt;span style="color: blue"&gt;If&lt;/span&gt; x &amp;gt; &lt;span style="color: maroon"&gt;5&lt;/span&gt; &lt;span style="color: blue"&gt;Then&lt;/span&gt;
Label1:
            &lt;span style="color: blue"&gt;Dim&lt;/span&gt; y = &lt;span style="color: maroon"&gt;5&lt;/span&gt;
            &lt;span style="color: blue"&gt;Dim&lt;/span&gt; f = &lt;span style="color: blue"&gt;Function&lt;/span&gt;() y
        &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;If&lt;/span&gt;
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Message: error BC36597: 'GoTo Label1' is not valid because 'Label1' is inside a scope that defines a variable that is used in a lambda or query expression.&lt;/p&gt;
&lt;p&gt;If you look back at &lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/05/25/closures-in-vb-part-3-scope.aspx"&gt;Part 3&lt;/a&gt;&amp;nbsp;of this series you will see that a lot of work goes into initializing closures inside of a scope.&amp;nbsp; Unfortunately allowing a user to jump into a block that contains a closure makes respecting these rules very difficult.&amp;nbsp; In a even trivial example in makes the resulting code mostly unreadable.&amp;nbsp; We decided to disable this in Orcas and reconsider it in a future release.&lt;/p&gt;
&lt;p&gt;It is perfectly legal however to jump into any scope that is currently visible regardless of whether or not in contains a closure.&amp;nbsp; Because jumping into a visible scope does not affect the creation of a variable lifetime (just the ending), it does not add any complications to the code.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;5. Cannot mix "On Error Goto" and Closures&lt;/p&gt;
&lt;p&gt;Message: error BC36595: Method cannot contain both an 'On Error GoTo' statement and a lambda or query expression.&lt;/p&gt;
&lt;p&gt;Because of restriction #4 we must disable this scenario as well since it's very easy to hit this scenario with "On Error Goto".&amp;nbsp; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4267111" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Closures/default.aspx">Closures</category></item><item><title>Expression Evaluator Sample Released</title><link>http://blogs.msdn.com/jaredpar/archive/2007/08/01/expression-evaluator-sample-released.aspx</link><pubDate>Wed, 01 Aug 2007 19:37:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4172888</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/4172888.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=4172888</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=4172888</wfw:comment><description>&lt;P&gt;The new version of the Visual Studio 2008 Beta2 SDK was just released.&amp;nbsp; Among the many new samples is a basic expression evaluator sample.&amp;nbsp; This sample shows you how to add basic debugger support to a managed language in Visual Studio.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;This is the first version of the sample and mainly has support for locals and member expansion.&amp;nbsp; I'm planning on adding more features in future releases.&amp;nbsp; Since this is mainly to provide users with guidelines on how to build an EE,&amp;nbsp;any requests we get will be given a lot of consideration. &lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.microsoft.com/downloads/details.aspx?FamilyID=d9000e2c-bd3f-4717-a181-723960814e16&amp;amp;displaylang=en"&gt;http://www.microsoft.com/downloads/details.aspx?FamilyID=d9000e2c-bd3f-4717-a181-723960814e16&amp;amp;displaylang=en&lt;/A&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4172888" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/EESdk/default.aspx">EESdk</category></item><item><title>Coding Quiz: Anonymous Type Types</title><link>http://blogs.msdn.com/jaredpar/archive/2007/08/01/coding-quiz-anonymous-type-types.aspx</link><pubDate>Wed, 01 Aug 2007 18:34:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4170691</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/4170691.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=4170691</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=4170691</wfw:comment><description>&lt;p&gt;Question: How can you create a variable in VB which is typed as an anonymous type but not actually create an instance of that type?&lt;/p&gt; &lt;p&gt;Answer in comments.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Note, doing this is not particularly useful it came about while I was playing around with a feature a few days ago.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4170691" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Anonymous+Types/default.aspx">Anonymous Types</category></item><item><title>Closures in VB Part 5: Looping</title><link>http://blogs.msdn.com/jaredpar/archive/2007/07/26/closures-in-vb-part-5-looping.aspx</link><pubDate>Thu, 26 Jul 2007 19:39:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4068113</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/4068113.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=4068113</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=4068113</wfw:comment><description>&lt;p&gt;For previous articles in the series please see&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/04/27/closures-in-vb-part-1.aspx"&gt;Part 1: Introduction&lt;/a&gt;  &lt;li&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/05/03/closures-in-vb-part-2-method-calls.aspx"&gt;Part 2: Method Calls&lt;/a&gt;  &lt;li&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/05/25/closures-in-vb-part-3-scope.aspx"&gt;Part 3: Scope&lt;/a&gt;  &lt;li&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/06/15/closures-in-vb-part-4-variable-lifetime.aspx"&gt;Part 4: Variable Lifetime&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Once again sorry for the long delay between posts.&amp;nbsp;&lt;/p&gt; &lt;p&gt;Looping structures can cause unintended consequences when used with Lambda expressions.&amp;nbsp; The problem occurs because lambda expressions do not execute when they are constructed but rather when they are invoked.&amp;nbsp; For example take the following code.&amp;nbsp; &lt;/p&gt;&lt;pre&gt;    &lt;span style="color: blue"&gt;Sub&lt;/span&gt; LoopExample1()
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; list &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;New&lt;/span&gt; List(Of Func(Of &lt;span style="color: blue"&gt;Integer&lt;/span&gt;))
        &lt;span style="color: blue"&gt;For&lt;/span&gt; i = &lt;span style="color: maroon"&gt;0&lt;/span&gt; &lt;span style="color: blue"&gt;To&lt;/span&gt; &lt;span style="color: maroon"&gt;3&lt;/span&gt;
            list.Add(&lt;span style="color: blue"&gt;Function&lt;/span&gt;() i)
        &lt;span style="color: blue"&gt;Next&lt;/span&gt;

        &lt;span style="color: blue"&gt;For&lt;/span&gt; &lt;span style="color: blue"&gt;Each&lt;/span&gt; cur &lt;span style="color: blue"&gt;In&lt;/span&gt; list
            Console.Write(&lt;span style="color: maroon"&gt;"{0} "&lt;/span&gt;, cur())
        &lt;span style="color: blue"&gt;Next&lt;/span&gt;
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Many users are surprised to find out the above will print "4 4 4 4 ".&amp;nbsp; The reason goes back to my previous 2 posts on variable lifetime and scope.&amp;nbsp; All "For" and "For Each" blocks in Vb have 2 scopes.&amp;nbsp; &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Scope where iteration variables are defined 
&lt;li&gt;Body of the for loop&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;The first scope is entered only once no matter how many times the loop is executed.&amp;nbsp; The second is entered once per iteration of the loop.&amp;nbsp; Any iteration variables that are defined in a For/For Each loop are created in the first scope (in this case "i" and "cur").&amp;nbsp; Hence there is only one of those variables for every loop iteration and the lambda function lifts the single variable "i".&amp;nbsp; &lt;/p&gt;
&lt;p&gt;This has thrown off many users because the behavior works most of the time.&amp;nbsp; For instance if I switched the code to run "Console.Write" inside the first loop, it would print out&amp;nbsp;"0 1 2 3 "&amp;nbsp;as expected.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;To mitigate against this problem the above code will actually produce a warning in VB.&amp;nbsp; &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;warning BC42324: Using the iteration variable in a lambda expression may have unexpected results.&amp;nbsp; Instead, create a local variable within the loop and assign it the value of the iteration variable.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;There are two ways to fix this problem depending on the behavior you want.&amp;nbsp; If you see this warning and don't know if it affects you, the safest change is to do the following.&amp;nbsp; &lt;/p&gt;&lt;pre&gt;    &lt;span style="color: blue"&gt;Sub&lt;/span&gt; LoopExample2()
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; list &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;New&lt;/span&gt; List(Of Func(Of &lt;span style="color: blue"&gt;Integer&lt;/span&gt;))
        &lt;span style="color: blue"&gt;For&lt;/span&gt; iTemp = &lt;span style="color: maroon"&gt;0&lt;/span&gt; &lt;span style="color: blue"&gt;To&lt;/span&gt; &lt;span style="color: maroon"&gt;3&lt;/span&gt;
            &lt;span style="color: blue"&gt;Dim&lt;/span&gt; i = iTemp
            list.Add(&lt;span style="color: blue"&gt;Function&lt;/span&gt;() i)
        &lt;span style="color: blue"&gt;Next&lt;/span&gt;

        &lt;span style="color: blue"&gt;For&lt;/span&gt; &lt;span style="color: blue"&gt;Each&lt;/span&gt; cur &lt;span style="color: blue"&gt;In&lt;/span&gt; list
            Console.Write(&lt;span style="color: maroon"&gt;"{0} "&lt;/span&gt;, cur())
        &lt;span style="color: blue"&gt;Next&lt;/span&gt;
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;This will cause "i" to be created in the second scope.&amp;nbsp; Hence there will be a different value for every loop iteration and the code will print out&amp;nbsp;"0 1 2 3"&amp;nbsp;as expected.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;If you do want the code to print out "4 4 4 4 " then add "Dim i = 0" before the start of the loop.&amp;nbsp; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4068113" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Closures/default.aspx">Closures</category></item><item><title>Closures in VB Part 4: Variable Lifetime</title><link>http://blogs.msdn.com/jaredpar/archive/2007/06/15/closures-in-vb-part-4-variable-lifetime.aspx</link><pubDate>Fri, 15 Jun 2007 23:47:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3319883</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/3319883.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=3319883</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=3319883</wfw:comment><description>&lt;p&gt;For previous articles in this series please see  &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/04/27/closures-in-vb-part-1.aspx"&gt;Part 1: Introduction&lt;/a&gt;  &lt;li&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/05/03/closures-in-vb-part-2-method-calls.aspx"&gt;Part 2: Method Calls&lt;/a&gt;  &lt;li&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/05/25/closures-in-vb-part-3-scope.aspx"&gt;Part 3: Scope&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Sorry for the long delay between posts here.&amp;nbsp; We're getting Orcas out the door and getting this series completed takes a back door to shipping.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Originally I wanted to talk about looping structures next.&amp;nbsp; However when I started writing that post I realized that I had to talk about lifetime before the looping structures would make sense.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Prior to Orcas the lifetime of a variable in VB was the entire function.&amp;nbsp; This presented several problems from a closures perspective.&amp;nbsp; Imagine you had a looping structure and the value was used in a lambda expression.&amp;nbsp; &lt;/p&gt;&lt;pre&gt;    &lt;span style="color: blue"&gt;Sub&lt;/span&gt; LifetimeExample()
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; list &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;New&lt;/span&gt; List(Of Func(Of &lt;span style="color: blue"&gt;Integer&lt;/span&gt;))
        &lt;span style="color: blue"&gt;For&lt;/span&gt; i = &lt;span style="color: maroon"&gt;0&lt;/span&gt; &lt;span style="color: blue"&gt;To&lt;/span&gt; &lt;span style="color: maroon"&gt;5&lt;/span&gt;

            &lt;span style="color: blue"&gt;Dim&lt;/span&gt; x = i * &lt;span style="color: maroon"&gt;2&lt;/span&gt;
            &lt;span style="color: blue"&gt;If&lt;/span&gt; &lt;span style="color: maroon"&gt;True&lt;/span&gt; &lt;span style="color: blue"&gt;Then&lt;/span&gt;
                list.Add(&lt;span style="color: blue"&gt;Function&lt;/span&gt;() x)
            &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;If&lt;/span&gt;
        &lt;span style="color: blue"&gt;Next&lt;/span&gt;

        &lt;span style="color: blue"&gt;For&lt;/span&gt; &lt;span style="color: blue"&gt;Each&lt;/span&gt; f &lt;span style="color: blue"&gt;In&lt;/span&gt; list
            Console.Write(f() &amp;amp; " ")
        &lt;span style="color: blue"&gt;Next&lt;/span&gt;
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In this example if we left the lifetime rules unchanged, there would be a single variable "x" for the entire function.&amp;nbsp; That means that we would end up printing out &lt;/p&gt;
&lt;p&gt;10 10 10 10 10&lt;/p&gt;
&lt;p&gt;This is somewhat unexpected and essentially means that VB could not support complex Lambda scenarios.&amp;nbsp; To fix this we altered the lifetime of variables to be tied to the scope they were contained in.&amp;nbsp; The end effect is that each iteration of the loop has a separate "x" since each iteration enters and leaves the scope of the "if" statement.&amp;nbsp; As a result it will print out &lt;/p&gt;
&lt;p&gt;0 2 4 6 8 10&lt;/p&gt;
&lt;p&gt;We did make one backcompat adjustment for this change.&amp;nbsp; The lifetime of variables in VB was visible if you tried to use an uninitialized variable in a loop/goto.&amp;nbsp; For instance the following code will also print out 0 2 4 6 8 10 because it takes advantage of the fact that the variable "x" has a lifetime longer than the loop.&lt;/p&gt;&lt;pre&gt;    &lt;span style="color: blue"&gt;Sub&lt;/span&gt; VisibleLifetime()
        &lt;span style="color: blue"&gt;For&lt;/span&gt; i = &lt;span style="color: maroon"&gt;0&lt;/span&gt; &lt;span style="color: blue"&gt;To&lt;/span&gt; &lt;span style="color: maroon"&gt;5&lt;/span&gt;
            &lt;span style="color: blue"&gt;Dim&lt;/span&gt; x &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;
            Console.WriteLine(x)
            x += &lt;span style="color: maroon"&gt;2&lt;/span&gt;
        &lt;span style="color: blue"&gt;Next&lt;/span&gt;
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;To make sure that we didn't break any existing code we had one little errata for the change.&amp;nbsp; When a variable's scope is re-entered, and hence recreated, and it is not initialized to a value it will get the last value of the variable.&amp;nbsp; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3319883" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Closures/default.aspx">Closures</category></item><item><title>Closures in VB Part 3: Scope</title><link>http://blogs.msdn.com/jaredpar/archive/2007/05/25/closures-in-vb-part-3-scope.aspx</link><pubDate>Fri, 25 May 2007 23:59:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2880594</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/2880594.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=2880594</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=2880594</wfw:comment><description>&lt;p&gt;For previous articles in this series please see&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/04/27/closures-in-vb-part-1.aspx"&gt;Part 1: Introduction&lt;/a&gt;  &lt;li&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/05/03/closures-in-vb-part-2-method-calls.aspx"&gt;Part 2: Method Calls&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Thus far in the series we've only lifted variables that are declared in the same block/scope.&amp;nbsp;What happens if we lift variables in different scope?&amp;nbsp; The answer is that one closure class will be created for every unique scope where a lifted variable is declared and all of the variables in that scope that are lifted will be placed in that closure.&amp;nbsp; Once again, examples speak best&lt;/p&gt;&lt;pre&gt;    &lt;span style="color: blue"&gt;Sub&lt;/span&gt; Scope1()
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; x = &lt;span style="color: maroon"&gt;5&lt;/span&gt;
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; f1 = &lt;span style="color: blue"&gt;Function&lt;/span&gt;(&lt;span style="color: blue"&gt;ByVal&lt;/span&gt; z &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;) x + z
        Console.WriteLine(f1(&lt;span style="color: maroon"&gt;5&lt;/span&gt;))
        &lt;span style="color: blue"&gt;If&lt;/span&gt; x &amp;gt; &lt;span style="color: maroon"&gt;2&lt;/span&gt; &lt;span style="color: blue"&gt;Then&lt;/span&gt;
            &lt;span style="color: blue"&gt;Dim&lt;/span&gt; y = &lt;span style="color: maroon"&gt;6&lt;/span&gt;
            &lt;span style="color: blue"&gt;Dim&lt;/span&gt; g = &lt;span style="color: maroon"&gt;7&lt;/span&gt;
            &lt;span style="color: blue"&gt;Dim&lt;/span&gt; f2 = &lt;span style="color: blue"&gt;Function&lt;/span&gt;(&lt;span style="color: blue"&gt;ByVal&lt;/span&gt; z &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;) z + y + g
            Console.WriteLine(f2(&lt;span style="color: maroon"&gt;4&lt;/span&gt;))
        &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;If&lt;/span&gt;
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The code will end up looking like so ...&lt;/p&gt;&lt;pre&gt;    &lt;span style="color: blue"&gt;Class&lt;/span&gt; Closure1
        &lt;span style="color: blue"&gt;Public&lt;/span&gt; x &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;

        &lt;span style="color: blue"&gt;Function&lt;/span&gt; Lambda_f1(&lt;span style="color: blue"&gt;ByVal&lt;/span&gt; z &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;)
            &lt;span style="color: blue"&gt;Return&lt;/span&gt; x + z
        &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Function&lt;/span&gt;

    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Class&lt;/span&gt;

    &lt;span style="color: blue"&gt;Class&lt;/span&gt; Closure2
        &lt;span style="color: blue"&gt;Public&lt;/span&gt; y &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;
        &lt;span style="color: blue"&gt;Public&lt;/span&gt; g &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;

        &lt;span style="color: blue"&gt;Function&lt;/span&gt; Lambda_f2(&lt;span style="color: blue"&gt;ByVal&lt;/span&gt; z &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;)
            &lt;span style="color: blue"&gt;Return&lt;/span&gt; y + z + g
        &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Function&lt;/span&gt;
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Class&lt;/span&gt;

    &lt;span style="color: blue"&gt;Sub&lt;/span&gt; Scope1()
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; c1 &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;New&lt;/span&gt; Closure1()
        c1.x = &lt;span style="color: maroon"&gt;5&lt;/span&gt;
        Console.WriteLine(c1.Lambda_f1(&lt;span style="color: maroon"&gt;5&lt;/span&gt;))
        &lt;span style="color: blue"&gt;If&lt;/span&gt; c1.x &amp;gt; &lt;span style="color: maroon"&gt;2&lt;/span&gt; &lt;span style="color: blue"&gt;Then&lt;/span&gt;
            &lt;span style="color: blue"&gt;Dim&lt;/span&gt; c2 &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;New&lt;/span&gt; Closure2()
            c2.y = &lt;span style="color: maroon"&gt;6&lt;/span&gt;
            c2.g = &lt;span style="color: maroon"&gt;7&lt;/span&gt;
            Console.WriteLine(c2.Lambda_f2(&lt;span style="color: maroon"&gt;4&lt;/span&gt;))
        &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;If&lt;/span&gt;
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;There are a couple of items to take away from this example.&amp;nbsp; &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Only two closure classes were created even though three variables were lifted.&amp;nbsp; The number of closures only depends on the number of scopes of all of the lifted declared variables. 
&lt;li&gt;The closures are created at the begining of the scope they are associated and not at the begining of the method.&amp;nbsp; This will be more important in the next part of the series.
&lt;li&gt;Each lambda instance is attached to the closure associated with the scope the lambda is declared in.&amp;nbsp; &lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;The next&amp;nbsp;twist is what were to happen if&amp;nbsp;the lambda "f2" were to also use the variable "x".&amp;nbsp; As it's currently written there is no association between Closure1 and Closure2 therefore&amp;nbsp;there is no way for it to access the lifted variable.&amp;nbsp; The answer is two fold.&amp;nbsp;&amp;nbsp;Firstly to&amp;nbsp;reduce clutter I pasted the closure classes as if they were separate entries.&amp;nbsp; In fact Closure2 would appear as a nested class of Closure1 in the real generated code.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Secondly if x were used inside of "f2", the real use would be "c1.x".&amp;nbsp; That's (almost) no different than "someOtherVar.x".&amp;nbsp; Therefore the instance of c1 will be lifted into Closure2.&amp;nbsp; &lt;/p&gt;&lt;pre&gt;&lt;span style="color: blue"&gt;Dim&lt;/span&gt; f2 = &lt;span style="color: blue"&gt;Function&lt;/span&gt;(&lt;span style="color: blue"&gt;ByVal&lt;/span&gt; z &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;) z + y + g + x&lt;/pre&gt;
&lt;p&gt;Woud result in the following definition of Closure2 ...&lt;/p&gt;&lt;pre&gt;    &lt;span style="color: blue"&gt;Class&lt;/span&gt; Closure2
        &lt;span style="color: blue"&gt;Public&lt;/span&gt; y &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;
        &lt;span style="color: blue"&gt;Public&lt;/span&gt; g &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;
        &lt;span style="color: blue"&gt;Public&lt;/span&gt; c1 &lt;span style="color: blue"&gt;As&lt;/span&gt; Closure1

        &lt;span style="color: blue"&gt;Function&lt;/span&gt; Lambda_f2(&lt;span style="color: blue"&gt;ByVal&lt;/span&gt; z &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;)
            &lt;span style="color: blue"&gt;Return&lt;/span&gt; y + z + g + c1.x
        &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Function&lt;/span&gt;
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Class&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In deeply nested lambdas and scopes this type of lifting will continue recursively.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;That's it for this entry, the next article will talk about looping structures and possibly variable lifetime.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2880594" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Closures/default.aspx">Closures</category></item><item><title>Closures in VB Part 2: Method Calls</title><link>http://blogs.msdn.com/jaredpar/archive/2007/05/03/closures-in-vb-part-2-method-calls.aspx</link><pubDate>Fri, 04 May 2007 00:56:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2400211</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/2400211.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=2400211</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=2400211</wfw:comment><description>&lt;p&gt;For previous articles in this series, please see&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/04/27/closures-in-vb-part-1.aspx"&gt;Part 1 - The basics&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;This part of the series will focus on how method calls are handled in closures.&amp;nbsp;&amp;nbsp;As stated in the previous article, the purpose of closures is to allow all operations inside a lambda or query expression that would normally be available inside the function or sub.&amp;nbsp; To do this closures often need to capture (or lift) relevant variables from the function into the generated class.&lt;/p&gt; &lt;p&gt;There are&amp;nbsp;2 types of methods and method calls that closures have to handle.&amp;nbsp; &lt;/p&gt; &lt;ol&gt; &lt;li&gt;Method calls to a shared method or methods on modules.  &lt;li&gt;Method calls to instance members of a class &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;&lt;strong&gt;Scenario #1 &lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Below is an example of a method call inside a lambda expression for scenario #1.&amp;nbsp; &lt;/p&gt;&lt;pre&gt;&lt;span style="color: blue"&gt;Module&lt;/span&gt; M1

    &lt;span style="color: blue"&gt;Function&lt;/span&gt; MyValue() &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;
        &lt;span style="color: blue"&gt;Return&lt;/span&gt; &lt;span style="color: maroon"&gt;42&lt;/span&gt;
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Function&lt;/span&gt;

    &lt;span style="color: blue"&gt;Sub&lt;/span&gt; Example1()
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; x = &lt;span style="color: maroon"&gt;5&lt;/span&gt;
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; f = &lt;span style="color: blue"&gt;Function&lt;/span&gt;() x + MyValue()
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;

&lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Module&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here we are calling a&amp;nbsp;module method inside a lambda.&amp;nbsp; Module Methods or Shared methods can be called from anywhere because they require no specific variable for the call.&amp;nbsp; This requires no special work from closures as the call can just be made naturally.&lt;/p&gt;&lt;pre&gt;    &lt;span style="color: blue"&gt;Class&lt;/span&gt; Closure
        &lt;span style="color: blue"&gt;Private&lt;/span&gt; x &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;

        &lt;span style="color: blue"&gt;Function&lt;/span&gt; Lambda_f() &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;
            &lt;span style="color: blue"&gt;Return&lt;/span&gt; x + M1.MyValue
        &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Function&lt;/span&gt;
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Class&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Scenario #2&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Calling an instance method is more difficult than a shared method because it requires the referenc "Me".&amp;nbsp; If you don't type this specifically in code the VB Compiler will add it for you under the hood.&amp;nbsp; To make this work the closures code will also "lift" the variable "Me" in the same way that it lifts normal variables in a function.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Calling a instance method inside a lambda expression is little difference than calling a member method on a variable used in a lambda.&amp;nbsp; The only difference is the variable is "Me".&amp;nbsp; For example&lt;/p&gt;&lt;pre&gt;&lt;span style="color: blue"&gt;Class&lt;/span&gt; C1
    &lt;span style="color: blue"&gt;Private&lt;/span&gt; m_myValue &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;

    &lt;span style="color: blue"&gt;Function&lt;/span&gt; MyValue() &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;
        &lt;span style="color: blue"&gt;Return&lt;/span&gt; m_myValue
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Function&lt;/span&gt;

    &lt;span style="color: blue"&gt;Sub&lt;/span&gt; Example2()
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; x = &lt;span style="color: maroon"&gt;5&lt;/span&gt;
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; f = &lt;span style="color: blue"&gt;Function&lt;/span&gt;() x + MyValue()
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;
&lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Class&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In this case we need to access both "x" and "Me.MyValue()" from the closure.&amp;nbsp; The generated code will create space for both of these variables and the transformed code in Example2 will store both of the values.&lt;/p&gt;&lt;pre&gt;&lt;span style="color: blue"&gt;Class&lt;/span&gt; Closure
    &lt;span style="color: blue"&gt;Private&lt;/span&gt; x &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Integer&lt;/span&gt;
    &lt;span style="color: blue"&gt;Private&lt;/span&gt; OriginalMe &lt;span style="color: blue"&gt;As&lt;/span&gt; C1

    &lt;span style="color: blue"&gt;Function&lt;/span&gt; Lambda_f()
        &lt;span style="color: blue"&gt;Return&lt;/span&gt; x + OriginalMe.MyValue()
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Function&lt;/span&gt;
&lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Class&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;    &lt;span style="color: blue"&gt;Sub&lt;/span&gt; Example2()
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; c &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;New&lt;/span&gt; Closure
        c.x = &lt;span style="color: maroon"&gt;5&lt;/span&gt;
        c.OriginalMe = &lt;span style="color: blue"&gt;Me&lt;/span&gt;
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; f = &lt;span style="color: blue"&gt;New&lt;/span&gt; Func(Of &lt;span style="color: blue"&gt;Integer&lt;/span&gt;)(&lt;span style="color: blue"&gt;AddressOf&lt;/span&gt; c.Lambda_f)
    &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;As usual, the generated code is much uglier but this essentially what will be generated.&amp;nbsp; That wraps it up for method calls.&amp;nbsp; In the next part, I will discuss the variable liftetime and scoping issues that come into play with closures.&amp;nbsp; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2400211" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Closures/default.aspx">Closures</category></item><item><title>Closures in VB: Part 1</title><link>http://blogs.msdn.com/jaredpar/archive/2007/04/27/closures-in-vb-part-1.aspx</link><pubDate>Sat, 28 Apr 2007 01:50:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2304918</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/2304918.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=2304918</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=2304918</wfw:comment><description>&lt;P&gt;One of the features I implemented for VB 9.0 is lexical closure support.&amp;nbsp; This a great addition to the VB language and I wanted to do a series of blog posts to describe this feature and how it will impact your code.&lt;/P&gt;
&lt;P&gt;Lexical Closures (more often referred to as simply Closures) are the underpinnings for several new features in Visual Basic 9.0.&amp;nbsp; The are part of the guts of Lambda and Query expressions.&amp;nbsp; This will be a several part series on Closures in VB 9.0; how they work, their limitations, pitfalls surrounding their use.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;To start off, let's get a basic summary of what a Closure is.&amp;nbsp; &lt;A href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29" target=_blank mce_href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29"&gt;Wikipedia&lt;/A&gt; defines it as "... a&amp;nbsp; is a semantic concept referring to a function paired with an environment ...".&amp;nbsp; I prefer to describe it as follows.&amp;nbsp; A closure is a feature which allows users to seemlessly access an environment (locals, parameters&amp;nbsp;and methods) from more than one function.&amp;nbsp; Even better are samples :) &lt;/P&gt;&lt;PRE&gt;    &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt; C1
        &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; Test()
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; x = &lt;SPAN style="COLOR: maroon"&gt;5&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; f = &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; y &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;) x + y
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; result = f(&lt;SPAN style="COLOR: maroon"&gt;42&lt;/SPAN&gt;)
        &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;

    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;In this code we have a lambda expression which takes in a single parameter and adds it with a local variable.&amp;nbsp; Lambda expressions are implemented as functions in VB (and C#).&amp;nbsp; So now we have two functions, "Test" and "f", which are accessing a single local variable.&amp;nbsp; This is where closures come into play.&amp;nbsp; Closures are responsible for making the single variable "x" available to both functions in a process that is referred to as "lifting the variable".&lt;/P&gt;
&lt;P&gt;To do this the compiler will take essentially&amp;nbsp;4 actions.&amp;nbsp; &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a class which will contain "x" in order to share it among both functions.&amp;nbsp; Call it "Closure" for now 
&lt;LI&gt;It will create a new function for the lambda expression in the class "Closure".&amp;nbsp; Call it "f" for now 
&lt;LI&gt;Create a new instance of the class "Closure" inside the sub "Test" 
&lt;LI&gt;Rewrite all access of "x" into the member "x" of "Closure".&lt;/LI&gt;&lt;/OL&gt;&lt;PRE&gt;    &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt; Closure
        &lt;SPAN style="COLOR: blue"&gt;Public&lt;/SPAN&gt; x &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;

        &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt; f(&lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; y &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;) &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: blue"&gt;Return&lt;/SPAN&gt; x + y
        &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;
    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt;

    &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt; C1
        &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; Test()
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; c &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;New&lt;/SPAN&gt; Closure()
            c.x = &lt;SPAN style="COLOR: maroon"&gt;5&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; f &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; Func(Of &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;Integer&lt;/SPAN&gt;) = &lt;SPAN style="COLOR: blue"&gt;AddressOf&lt;/SPAN&gt; c.f
            &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; result = f(&lt;SPAN style="COLOR: maroon"&gt;42&lt;/SPAN&gt;)
        &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;

    &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;Now "x" is shared amongst both functions and the user didn't have to know anything about the code we generated.&amp;nbsp; You can see from this simplified example just how much code Closures and all of the other new VB 9.0 features are saving you here (Type Inference, Lambda Expressions).&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Note this is only a simulation of what is generated when you use a closure, the actual generated code is much uglier and involves lots of unbindable names "$Lambda_1", etc ... &lt;/P&gt;
&lt;P&gt;In the next part of this article I'll dive into some more uses of closures (multiple variables, method access, &amp;nbsp;terminology, etc...).&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2304918" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Closures/default.aspx">Closures</category></item></channel></rss>