0

Imagine an InvoiceHeaderGrid and an InvoiceDetailGrid.

Lets's say we have this code in a HeaderGrid's rowchanged event:

    DataTable InvoiceDetail = new GetInvoiceDetail(HeaderGrid.CurrentId);
    InvoiceDetail.ItemDeleted += (sender, evt) =>
     {

     };
     DetailGrid.DataSource = InvoiceDetail;

so that the code is called every time the user focuses a different row in the HeaderGrid.

The "dead" InvoiceDetail datatable and its attached anonymous handler get garbage-collected with each reassignment of variable InvoiceDetail, right? The attached handler isn't treated as a reference to the object that would prevent it from being garbage-collected?

Tim
  • 8,669
  • 31
  • 105
  • 183
  • 1
    it is not garbage collected as soon as it is reassign......it is available for collection and will be collected when next time garbage collection runs...When garbage collection will run is non deterministic – Viru Jan 31 '16 at 18:42
  • An event subscription can interfere with GC, but not in the way you seem to be worried about. If the object that publishes the event is unreachable, then so too are any event subscribers that are only attached to the event, and the publisher and subscriber can both be GC'ed. See the marked duplicate for additional details. – Peter Duniho Jan 31 '16 at 18:44

0 Answers0