<?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 : DotNet</title><link>http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx</link><description>Tags: DotNet</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>BclExtras Library</title><link>http://blogs.msdn.com/jaredpar/archive/2009/01/23/bclextras-library.aspx</link><pubDate>Fri, 23 Jan 2009 16:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9372192</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9372192.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9372192</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9372192</wfw:comment><description>&lt;p&gt;I published a .Net utility library on &lt;a href="http://code.msdn.microsoft.com"&gt;Code Gallery&lt;/a&gt; today called &lt;a href="http://code.msdn.microsoft.com/BclExtras"&gt;BclExtras&lt;/a&gt;.&amp;#160; It’s a set of classes meant to be used in addition to the standard .Net base class libraries (BCL).&amp;#160; The main focuses of the library are functional programming, multi-threading, LINQ extensions, unit testing and API’s designed to support type inference.&lt;/p&gt;  &lt;p&gt;This project evolved from various classes and constructs I was using in personal projects.&amp;#160; For the last year or so I’ve kept as a separate tested library.&amp;#160; It started out with a lot of multi-threaded code constructs but lately is leaning to a lot of functional style API’s and collections.&amp;#160; &lt;/p&gt;  &lt;p&gt;The library includes source and binaries for .Net 2.0 and .Net 3.5.&amp;#160; The .Net 2.0 version of the library includes many constructs added in 3.5 that don’t rely on any 2.0SP1 CLR features.&amp;#160; Examples are Func&amp;lt;T&amp;gt;, Action&amp;lt;T&amp;gt;, the Extension attribute and a subset of the LINQ Enumerable class.&amp;#160; It allows for most LINQ expressions in a 2.0 targeted application. These types are removed in the 3.5 version to avoid conflicts with types in System.Core.&amp;#160; &lt;/p&gt;  &lt;p&gt;I’ve previously released this library under the name &lt;a href="http://code.msdn.microsoft.com/RantPack"&gt;RantPack&lt;/a&gt;.&amp;#160; It originally started out as personal utility library of mine and hence ended up with the somewhat obscure name.&amp;#160; But, besides to me, RantPack doesn’t really convey a useful meaning.&amp;#160; So I decided to give it a more meaningful name for the general population.&amp;#160; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9372192" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</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/RantPack/default.aspx">RantPack</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/BclExtras/default.aspx">BclExtras</category></item><item><title>Is IntPtr(long) truncating?</title><link>http://blogs.msdn.com/jaredpar/archive/2008/10/28/is-intptr-long-truncating.aspx</link><pubDate>Tue, 28 Oct 2008 15:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9018607</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9018607.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9018607</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9018607</wfw:comment><description>&lt;p&gt;The short answer is: No, not when it matters&lt;/p&gt;  &lt;p&gt;A colleague and I were discussing a particular scenario around IntPtr,PInvoke and 64 bit correctness.&amp;#160; Eventually our discussion lead us to the IntPtr constructor which takes a long.&amp;#160; To my surprise the code for the constructor is the following.&lt;/p&gt;  &lt;p&gt;public unsafe &lt;b&gt;&lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.IntPtr/.ctor(Int64)"&gt;IntPtr&lt;/a&gt;&lt;/b&gt;(&lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Int64"&gt;long&lt;/a&gt; value) { this.&lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.IntPtr/m_value:Void*"&gt;m_value&lt;/a&gt; = (&lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Void"&gt;void&lt;/a&gt;*) ((&lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Int32"&gt;int&lt;/a&gt;) &lt;a&gt;value&lt;/a&gt;); }&lt;/p&gt;  &lt;p&gt;The problem is long value is arbitrarily truncated to an int.&amp;#160; This has the effect of essentially losing any address over the 4 GB range (in other words, no 64 bit addresses).&amp;#160; This much to big of a hole to actually be the real behavior so I decided to see if it was a bug in the disassembler.&amp;#160; I was using .Net Reflector so I switched to IL mode.&amp;#160; &lt;/p&gt;  &lt;pre&gt;    L_0000: ldarg.0 
    L_0001: ldarg.1 
    L_0002: conv.ovf.i4 
    L_0003: conv.i 
    L_0004: stfld void* System.IntPtr::m_value
    L_0009: ret &lt;/pre&gt;

&lt;p&gt;This confirmed it is indeed truncating the value (and doing an overflow check to boot). But wait, mscorlib.dll is a processor specific DLL so perhaps this is just a 32 bit OS thing.&amp;#160; I switched over to a 64 bit machine, fired up Reflctor and found to my dismay that it had the exact same code.&amp;#160; &lt;/p&gt;

&lt;p&gt;After a few minutes I thought to open up task manager and to my surprise reflector was running in a WoW64 bit process.&amp;#160; This meant it was still loading up the 32 bit version of mscorlib.dll.&amp;#160; Next I fired up ildasm, loaded up a 64 bit mscorlib and confirmed that the code will not truncate on 64 bit machines. &lt;/p&gt;

&lt;pre&gt;  IL_0000:  ldarg.0
  IL_0001:  ldarg.1
  IL_0002:  conv.u
  IL_0003:  stfld      void* System.IntPtr::m_value
  IL_0008:  ret&lt;/pre&gt;

&lt;p&gt;The conv.u code is a conversion to unsigned native platform int. On a 64 bit machine this will be an unsigned 8 byte number(see &lt;a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.conv_u.aspx"&gt;OpCodes.Conv_U&lt;/a&gt; for more details).&amp;#160; &lt;/p&gt;

&lt;p&gt;So what does this mean for the developer.&amp;#160; Essentially IntPtr(long) will do the right thing independently of the platform a developer is using.&amp;#160; On a 32 bit platform it will (correctly) throw exceptions if a non-4GB address is passed in.&amp;#160; In 64 bit land it will essentially do nothing and rely on the programmer to give correct addresses.&amp;#160; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9018607" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/PInvoke/default.aspx">PInvoke</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Wow64/default.aspx">Wow64</category></item><item><title>RantPack - A utility library</title><link>http://blogs.msdn.com/jaredpar/archive/2008/04/30/rantpack-a-utility-library.aspx</link><pubDate>Thu, 01 May 2008 06:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8446131</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/8446131.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=8446131</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=8446131</wfw:comment><description>&lt;P&gt;I often post code examples, samples and snippets on this blog.&amp;nbsp; Many of these samples are a part of a utility library I've been writing and maintaining for many years now.&amp;nbsp; Essentially since I got involved in DotNet programming.&lt;/P&gt;
&lt;P&gt;I write a lot of code for&amp;nbsp;internal apps, demos&amp;nbsp;and hobby projects.&amp;nbsp; Having a utility library to tote around with me greatly increases my impact in a project because it gives me a place to put common code that I would otherwise have to repeatedly cut and paste into projects.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Whenever I work on something I think the community at large would benefit from I try to blog about it.&amp;nbsp; Unfortunately for the more complex samples blogging is not always easy.&amp;nbsp; Complex structures don't really fit on a blog and often utilities depend on utilities and it forces users to cut and paste between multiple blog entries.&amp;nbsp; Not good. &lt;/P&gt;
&lt;P&gt;Now with &lt;A href="http://code.msdn.com/" mce_href="http://code.msdn.com"&gt;http://code.msdn.com&lt;/A&gt; I have a forum to fully share the items I've been working on.&amp;nbsp; This is the perfect place to share code for the community and helps avoid the messiness of cutting and pasting blog posts.&amp;nbsp; As such I've released my Library, &lt;A href="http://code.msdn.microsoft.com/RantPack" mce_href="http://code.msdn.microsoft.com/RantPack"&gt;RantPack&lt;/A&gt;, on Code Gallery.&amp;nbsp; This includes both binaries and sources and is covered under the &lt;A href="http://code.msdn.microsoft.com/RantPack/Project/License.aspx" mce_href="http://code.msdn.microsoft.com/RantPack/Project/License.aspx"&gt;Microsoft Public License&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;RantPack is available at &lt;A title=http://code.msdn.microsoft.com/RantPack href="http://code.msdn.microsoft.com/RantPack" mce_href="http://code.msdn.microsoft.com/RantPack"&gt;http://code.msdn.microsoft.com/RantPack&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;A couple of notes on the sample. This is mainly for education and sharing purposes.&amp;nbsp; The goal of this library is to make developing software faster and more reliable.&amp;nbsp; It includes, but is not limited to &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Functional Programming Patterns&lt;/LI&gt;
&lt;UL&gt;
&lt;LI&gt;Tuples&lt;/LI&gt;
&lt;LI&gt;Immutable/Persistent collections&lt;/LI&gt;&lt;/UL&gt;
&lt;LI&gt;Threading &lt;/LI&gt;
&lt;UL&gt;
&lt;LI&gt;Futures&lt;/LI&gt;
&lt;LI&gt;Active Objects&lt;/LI&gt;
&lt;LI&gt;Various other primitives&lt;/LI&gt;&lt;/UL&gt;
&lt;LI&gt;General Patterns&lt;/LI&gt;
&lt;LI&gt;General Utilities&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Another item you will find browsing the sources are tests.&amp;nbsp; Lots and lots of tests.&amp;nbsp; At the time of this writing there are around 400 some odd unit tests on the library.&amp;nbsp; There is nothing more frustrating than a utility library that doesn't work.&amp;nbsp; As such I rigorously test the code I write. &lt;/P&gt;
&lt;P&gt;Although I performance test much of my code and it performs great in the applications I use it for I'm sure there are a few cases I missed.&amp;nbsp; Please feel free to point this out to me and I'll look into them.&lt;/P&gt;
&lt;P&gt;Why the name RantPack?&amp;nbsp; My blog is Rantings of a programmer and it's a pack of code :)&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8446131" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/RantPack/default.aspx">RantPack</category></item><item><title>IEnumerable and IEnumerable(Of T) 2</title><link>http://blogs.msdn.com/jaredpar/archive/2007/10/05/ienumerable-and-ienumerable-of-t-2.aspx</link><pubDate>Fri, 05 Oct 2007 18:43:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5294892</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/5294892.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=5294892</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=5294892</wfw:comment><description>&lt;p&gt;Quick follow up to my earlier &lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/10/04/ienumerable-and-ienumerable-of-t.aspx"&gt;post&lt;/a&gt;.&amp;nbsp; Fixing this issue in C# is even easier because of the existence of iterators.&amp;nbsp; &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;static&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;object&lt;/span&gt;&amp;gt; Shim(System.Collections.&lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt; enumerable)
        {
            &lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; cur &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; enumerable)
            {
                &lt;span style="color: rgb(0,0,255)"&gt;yield&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; cur;
            }
        }
&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=5294892" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</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>.Net Framework Source Code</title><link>http://blogs.msdn.com/jaredpar/archive/2007/10/03/net-framework-source-code.aspx</link><pubDate>Wed, 03 Oct 2007 21:58:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5266568</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/5266568.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=5266568</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=5266568</wfw:comment><description>&lt;p&gt;If you haven't heard the news yet, you can read the full article on &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/10/03/releasing-the-source-code-for-the-net-framework-libraries.aspx"&gt;ScottGu's Blog&lt;/a&gt;.&amp;nbsp; &lt;/p&gt; &lt;p&gt;In summary Microsoft has released the source code for the .Net Framework in such a way that you can step into the Framework while debugging.&amp;nbsp; IMHO this is great for users because it allows us to get a lot of insight into parts of the API that are tricky or not well documented.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Take a look at his blog for the full details of hooking this up into the debugger.&amp;nbsp; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5266568" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Debugging/default.aspx">Debugging</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>Detecting if you are an Admin</title><link>http://blogs.msdn.com/jaredpar/archive/2007/08/01/detecting-if-you-are-an-admin.aspx</link><pubDate>Wed, 01 Aug 2007 18:56:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4171063</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/4171063.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=4171063</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=4171063</wfw:comment><description>&lt;p&gt;This came up on an internal alias.&amp;nbsp; A customer wanted to know how to determine if there were running as an admin in a tool.&amp;nbsp; Below is a sample program that will print out whether or not you are the machine admin or a member of the machine administrators group.&amp;nbsp; &lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;This is essentially the same as the code in a previous post of mine but ported to VB.&amp;nbsp; &lt;a title="http://blogs.msdn.com/jaredpar/archive/2005/11/07/489942.aspx" href="http://blogs.msdn.com/jaredpar/archive/2005/11/07/489942.aspx"&gt;http://blogs.msdn.com/jaredpar/archive/2005/11/07/489942.aspx&lt;/a&gt;&lt;/p&gt;&lt;pre&gt;&lt;span style="color: blue"&gt;Imports&lt;/span&gt; System.Security.Principal
&lt;span style="color: blue"&gt;Module&lt;/span&gt; Module1

    &lt;span style="color: blue"&gt;Function&lt;/span&gt; IsRunningAsLocalAdmin() &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Boolean&lt;/span&gt;
        &lt;span style="color: blue"&gt;Dim&lt;/span&gt; cur &lt;span style="color: blue"&gt;As&lt;/span&gt; WindowsIdentity = WindowsIdentity.GetCurrent()
        &lt;span style="color: blue"&gt;For&lt;/span&gt; &lt;span style="color: blue"&gt;Each&lt;/span&gt; role &lt;span style="color: blue"&gt;As&lt;/span&gt; IdentityReference &lt;span style="color: blue"&gt;In&lt;/span&gt; cur.Groups
            &lt;span style="color: blue"&gt;If&lt;/span&gt; role.IsValidTargetType(&lt;span style="color: blue"&gt;GetType&lt;/span&gt;(SecurityIdentifier)) &lt;span style="color: blue"&gt;Then&lt;/span&gt;
                &lt;span style="color: blue"&gt;Dim&lt;/span&gt; sid &lt;span style="color: blue"&gt;As&lt;/span&gt; SecurityIdentifier = &lt;span style="color: blue"&gt;DirectCast&lt;/span&gt;(role.Translate(&lt;span style="color: blue"&gt;GetType&lt;/span&gt;(SecurityIdentifier)), SecurityIdentifier)
                &lt;span style="color: blue"&gt;If&lt;/span&gt; sid.IsWellKnown(WellKnownSidType.AccountAdministratorSid) &lt;span style="color: blue"&gt;OrElse&lt;/span&gt; sid.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid) &lt;span style="color: blue"&gt;Then&lt;/span&gt;
                    &lt;span style="color: blue"&gt;Return&lt;/span&gt; &lt;span style="color: maroon"&gt;True&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;If&lt;/span&gt;
        &lt;span style="color: blue"&gt;Next&lt;/span&gt;

        &lt;span style="color: blue"&gt;Return&lt;/span&gt; &lt;span style="color: maroon"&gt;False&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; Main()
        Console.WriteLine(&lt;span style="color: maroon"&gt;"Is Admin {0}"&lt;/span&gt;, IsRunningAsLocalAdmin())
    &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=4171063" 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/LUA/default.aspx">LUA</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</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>IMetaDataImport::GetParamForMethodIndex</title><link>http://blogs.msdn.com/jaredpar/archive/2007/07/30/imetadataimport-getparamformethodindex.aspx</link><pubDate>Mon, 30 Jul 2007 20:09:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4134295</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/4134295.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=4134295</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=4134295</wfw:comment><description>&lt;p&gt;While investigating a recent bug I found about an interesting return for GetParamForMethodIndex.&amp;nbsp; On a perfectly verifiable assembly, a call to GetParamForMethodIndex was returning a failure code.&amp;nbsp;After some searching I found the return code was&amp;nbsp;CLDB_E_RECORD_NOTFOUND. I was surprised at first because it's a verifiable assembly so how could the record for a parameter I knew existed not be there?&lt;/p&gt; &lt;p&gt;It turns out this is legal.&amp;nbsp; GetParamForMethodIndex returns a mdParamDef token by which you can query for information about a parameter with GetParamProps.&amp;nbsp; This will return the following information about a parameter.&amp;nbsp; &lt;/p&gt; &lt;ul&gt; &lt;li&gt;Name of the parameter&lt;/li&gt; &lt;li&gt;Attributes about the param (ByRef, Marshal, etc ...)&lt;/li&gt; &lt;li&gt;Default value&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;In this particular case the assembly was generated without a parameter name.&amp;nbsp; As it also had none of the information the parameter row was omitted from the metadata.&amp;nbsp; The reason is adding an empty row takes up space and provides no data.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Note you can't reproduce this behavior with ILASM.exe.&amp;nbsp; If you omit a parameter name, ILASM.exe will add one for you and hence generate a parameter row. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4134295" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/CLR/default.aspx">CLR</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/IMetaDataImport/default.aspx">IMetaDataImport</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><item><title>CoSetProxyBlanket and Managed Code</title><link>http://blogs.msdn.com/jaredpar/archive/2007/04/19/cosetproxyblanket-and-managed-code.aspx</link><pubDate>Thu, 19 Apr 2007 19:15:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2193730</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/2193730.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=2193730</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=2193730</wfw:comment><description>&lt;p&gt;When running FxCop on any managed code&amp;nbsp;that uses CoSetProxyBlanket you will see an error message saying the method cannot be called reliably from managed code.&amp;nbsp; I've hit that message before was frustrated by my attempts to find an explanation on the web.&amp;nbsp; Part of the reason is I'm not the most efficient searcher on the web.&amp;nbsp; &lt;/p&gt; &lt;p&gt;However and internal discussion revealed to me a great blog entry explaining exactly why this is unreliable and more importantly how to work around the issue.&lt;/p&gt; &lt;p&gt;&lt;a href="http://blogs.msdn.com/mbend/archive/2007/04/18/cosetproxyblanket-not-supported-from-managed-code.aspx"&gt;http://blogs.msdn.com/mbend/archive/2007/04/18/cosetproxyblanket-not-supported-from-managed-code.aspx&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;div class="wlWriterSmartContent" id="0767317B-992E-4b12-91E0-4F059A8CECA8:88620b15-bace-49fa-adf7-f99781def9fa" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati tags: &lt;a href="http://technorati.com/tags/DotNet" rel="tag"&gt;DotNet&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2193730" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/FxCop/default.aspx">FxCop</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Security/default.aspx">Security</category></item></channel></rss>