0

I have just been watching a TekPub video on Lambda's and the code was similar to such:

class Foo{
   void DoSomething();
}

static void Bar(Foo item, Action<Foo> action){
   return action.Invoke(item);
}

And then within Main:

Bar(new Foo(), x=>x.DoSomething();

My question is, is the Foo object just within scope for that call to Bar? Is the object destroyed once that method has been called?

Thanks.

4
  • Did you mean to write action.Invoke(Item) in Bar? Commented Feb 27, 2011 at 12:51
  • That code doesn't make sense. "Item" is never used, so there is no relationsship between "Item" and "action" (which you seem to imply with your question). Another thing: I assume "Foo" with large "F" is just a spelling error? And objects are (in general) garbage collected when the GC runs and there is no more reference to them. Commented Feb 27, 2011 at 12:57
  • You can't return FROM a void function, and using as the return value a void method! You can normally call your Action directly, like this: action(item) Commented Feb 27, 2011 at 13:05
  • @xanatos, yes off course I should have had the body of the method without the return side. Commented Feb 27, 2011 at 18:22

2 Answers 2

2

In this particular case, what happens is that the foo object is passed, along with your delegate, to the Bar method. The Bar method invokes the action, which calls DoSomething on foo, then returns.

Since the method Bar doesn't return the object you pass to it, nor the result of calling the delegate, and the code in question doesn't store the object reference anywhere, the object foo that you created is now eligible for garbage collection once Bar returns.

Exactly when memory for that object will be reclaimed depends on when GC runs, but at some point after Bar has returned, the memory allocated to the object will be reclaimed. It will not happen immediately, ie. as part of Bar returning.

Sign up to request clarification or add additional context in comments.

1 Comment

the delegate could add the object to a list or other object which would result in a reference, thus it won't be garbage collected. However, in this particulary case, you are right.
0

Yes, it should be disposed of once the method returns as none of the operations produce an additional reference.

However, this isn't the general case, it really depends on what the method does with it - if it creates a new object with a reference to the inline created one, it can live after the method returned. In this case the Action<T> could add the Foo to a dictionary or list of some sort which would mean that it won't be garbage collected, as there are still references to it.

1 Comment

"disposed" is something very different than "garbage collected". The question is not related to IDisposable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.