473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Anymous method and outer variables

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
Nov 17 '05 #1
1 1917
"Etienne Fortin" <Et***********@discussions.microsoft.com> wrote in message
news:F7**********************************@microsof t.com...
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

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Murat Tasan | last post by:
i have an inner class and an outer class, both of which have an identically named member. how from the inner class can i reference the outer member? (the example code should illustrate this...
0
by: Maarten van Reeuwijk | last post by:
I am constructing a "placeholder array" to mimick Numeric arrays. I have a 3D regular structured domain, so I can describe the axis with 3 1D arrays x, y and z. All the variables defined on this...
0
by: Cordell Lawrence | last post by:
Okay guys, We are wondering if this is a bug in Framework 2.0.40607 and looking for some clarification on the issue. Take a look at the folowing code. public delegate bool BoundryTest(int...
3
by: trialproduct2004 | last post by:
hi all i am having application which is using two classes. in one class i am creating instance of another class and calling method of that class. what i want is from inner class's method i want...
44
by: gregory.petrosyan | last post by:
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ????) print x All I want is to make self.data the default argument for self.f(). (I
10
by: Mihai Osian | last post by:
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. ...
13
by: Håkan Johansson | last post by:
Coming from Delphi, I've tried to declare a function within a method, but can't get the compiler to "swollow it". Is it at all possible? That is: SomeMethod() { LocalFunction() {
18
by: Tommy Nordgren | last post by:
Given the following: def outer(arg) avar = '' def inner1(arg2) # How can I set 'avar' here ? ------------------------------------- This sig is dedicated to the advancement of Nuclear Power...
2
by: Matt Nordhoff | last post by:
Sebastjan Trepca wrote: Python doesn't like when you read a variable that exists in an outer scope, then try to assign to it in this scope. (When you do "a = b", "b" is processed first. In...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.