Skip to main content

Antonio Cisternino's Home Page

Go Search
Home
  
Antonio Cisternino's Home Page > My Blog > More on CLR 2.0 closures  

My Blog: More on CLR 2.0 closures

Title

More on CLR 2.0 closures 

Body

In a previous post in my blog (http://rotor.di.unipi.it/cisterni/Lists/My%20Blog/DispForm.aspx?ID=8&Source=http://rotor.di.unipi.it/cisterni/Lists/My%2520Blog/AllItems.aspx) I mentioned the new support for implementing closures in CLR 2.0. The main point of that post was that now you can build a delegate using static methods and an object to be passed as the first argument of the method. This removes the limitation of delegate model of CLR v1.0 that requires the environment (the object)  and the method associated with it to be defined in the same class.
I started the post with a short introduction to closures using C#  2.0. It didn't occur to me that from my post one may conclude that C# closures are implemented with the support of this new CLR feature (thanks to Roshan James and his post - http://pensieve.thinkingms.com/CommentView,guid,9fe42970-09e3-44e2-a4d0-32d63139351a.aspx - about closures I realized it).
I completely agree with Roshan on how the so called anonmyous methods are implemented in C# 2.0. In his post, Roshan shows how C# compiler deals with closures so refer to that post for details.
However current anonymous methods implementation may be not so efficient in the real world because whenever you close a local variable into an anonymous method you pay the definition of a class into the runtime. In a scenario where closures are used extensively you may have unneeded overhead due to over-allocation of EEClasses into the runtime and JITting. Besides, using CLR closures instead you would save the definition of a class for each anonymous method reducing the number of classes introduced by the program. Moreover the implementation of the compiler would be more straightforward.
I guess that C# implemented anonymous methods before CLR closures were available and thus they cannot relay on this new mechanism. Nevertheless I hope that some day the C# compiler would change the compilation schema in favor of the newer mechanism.

Expires

 

Category

Programming 
Attachments
Created at 5/22/2004 10:35  by Antonio Cisternino 
Last modified at 5/22/2004 10:52  by Antonio Cisternino