Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Thu, 07 Jun 2007 16:18:08 -0700
On Thu, 07 Jun 2007 14:56:44 -0700, Jon Skeet [C# MVP] <skeet@xxxxxxxxx> wrote:
[...]
The problem is that the generated code uses a class <>c__Displayclass2
which has captured *both* "cheap" and "expensive", and longLived has a
reference to an instance of that class.
Ah. I see where my misunderstanding was. I somehow got the impression that the problem lay in the lifetime of the "shortLived" and "longLived" variables, when in fact it's the other local variables that they (via the anonymous delegates) reference that are of concern.
And as you explain, since the problem is that the two variables "cheap" and "expensive" are tied in lifetime by their being in the same scope, you get rid of the lifetime problem by moving the "expensive" variable into a different scope, so that the compiler can generate a new lifetime-managing class to deal with it separate from "cheap".
[...]
Does that make any kind of sense? (I'm really, really interested in how
best to explain this...)
I believe it does, now. As far as how to better explain it, I am not sure. However, I can offer the feedback as above, that it's important to make clear which local variables are affecting how lifetime is managed and in what way. In the example here, one of the issues is that the lifetime effects are illustrated through the use of new local variables, making it hard to distinguish which of the local variables are important in terms of determining lifetime.
Perhaps a better explanation would have had only two local variables, to ensure that it was clear which variables were important to the definition of lifetime for each?
Just a thought.
Pete
.
- Follow-Ups:
- Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- From: Jon Skeet [C# MVP]
- Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- References:
- Newbie Q: Declare variable IN the loop or BEFORE the loop?
- From: Rex
- Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- From: Marc Gravell
- Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- From: Jon Skeet [C# MVP]
- Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- From: Peter Duniho
- Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- From: Jon Skeet [C# MVP]
- Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- From: Peter Duniho
- Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- From: Jon Skeet [C# MVP]
- Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- From: Peter Duniho
- Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- From: Jon Skeet [C# MVP]
- Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- From: Peter Duniho
- Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- From: Jon Skeet [C# MVP]
- Newbie Q: Declare variable IN the loop or BEFORE the loop?
- Prev by Date: interfaces
- Next by Date: Re: compiler warnings for unconditional recursive calls
- Previous by thread: Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- Next by thread: Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- Index(es):
Relevant Pages
|