PC Review


Reply
Thread Tools Rate Thread

Anymous method and outer variables

 
 
=?Utf-8?B?RXRpZW5uZSBGb3J0aW4=?=
Guest
Posts: n/a
 
      25th Sep 2005
Hi,
In the .NET 2.0 Beta 2 documentation, the following is wrtten about
anonymous methods:
Unlike local variables, the lifetime of the outer variable extends until the
delegates that reference the anonymous methods are eligible to garbage
collection. The value of n is captured at the time the delegate is created.

The example shows an int that is used in the anonymous method block. That
int then becomes an outer variable it's value is captured at the creation of
the anonymous code block.

That's good with value type. But what about reference type? How is the value
"captured"?

Let's change the example.
int n = 0;
MyDel d = new MyDel() { Console.WriteLine("Copy #:{0}", ++n); };
To
MyClass n = new MyClass();
MyDel d = new MyDel() { Console.WriteLine("Copy #:{0}", n.SomeProperty); };

How is the value of n captured since it's a reference type? Will it compiles
at all?

Etienne Fortin
 
Reply With Quote
 
 
 
 
Richard Blewett [DevelopMentor]
Guest
Posts: n/a
 
      25th Sep 2005
"Etienne Fortin" <(E-Mail Removed)> wrote in message
news:F7E4DACA-F5DE-4E33-B4CE-(E-Mail Removed)...
> Hi,
> In the .NET 2.0 Beta 2 documentation, the following is wrtten about
> anonymous methods:
> Unlike local variables, the lifetime of the outer variable extends until
> the
> delegates that reference the anonymous methods are eligible to garbage
> collection. The value of n is captured at the time the delegate is
> created.
>
> The example shows an int that is used in the anonymous method block. That
> int then becomes an outer variable it's value is captured at the creation
> of
> the anonymous code block.
>
> That's good with value type. But what about reference type? How is the
> value
> "captured"?
>
> Let's change the example.
> int n = 0;
> MyDel d = new MyDel() { Console.WriteLine("Copy #:{0}", ++n); };
> To
> MyClass n = new MyClass();
> MyDel d = new MyDel() { Console.WriteLine("Copy #:{0}",
> n.SomeProperty); };
>
> How is the value of n captured since it's a reference type? Will it
> compiles
> at all?
>
> Etienne Fortin


The "local" variables you reference in the anonymous code block actually
become instance members on a compiler generated class (that also contains
the anonymous method) so it works just like any other member variable. This
means that things you regard as local variables, maybe on the stack, are
potentially being accessed by a hidden reference and are allocated on the
managed heap.

The other effect of this is that if you modify an outer variable in the
anonymous method the code outside of the anonymous method can see the
change - which has implications for thread synchronization

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Threading and outer-variable semantics, local variables capture inanonymous methods--why? RayLopez99 Microsoft C# .NET 3 15th Feb 2010 09:22 AM
OT: Sending Anymous E-Mails * * Chas Anti-Virus 51 16th Nov 2005 07:32 AM
Re: Sending Anymous E-Mails Anonymous via the Cypherpunks Tonga Remailer Anti-Virus 1 18th Oct 2005 02:01 AM
How can I refer to an outer method? Balint Toth Microsoft Dot NET Compact Framework 2 2nd Mar 2004 09:44 PM
Invoke method / Late binding / passing ref variables to a method?? PJ Microsoft C# .NET 0 25th Jul 2003 10:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:18 PM.