Search code examples
c#garbage-collectionobject-initialization

Garbage Collection on objects not assigned to a variable


I am working on project that uses this pattern

var businessEntity = new DAL().GetObject(id);
// do something with the business entity.

Has anyone followed this pattern?

Does this cause any memory management issues? Any complications with the garbage collector?

Thanks


Solution

  • It works just fine. It will be garbaged collected just fine. Depending on the implementation and the object, either at the end of the line it will be marked for collection, or once businessEntity goes out of scope.