2

Possible Duplicate:
When should weak references be used?

Hi,

When is it good practice to use weak references?

Could somebody provide an example?

Community
  • 1
  • 1
user712923
  • 1,475
  • 2
  • 16
  • 18

3 Answers3

1

An example where they could be used is in implementing a caching container.

Darin Dimitrov
  • 994,864
  • 265
  • 3,241
  • 2,902
1

CAB (and its open source equivalent, bbv.Common.EventBroker) uses weak references to allow loosely-coupled event subscriptions between forms (or their MVC controllers).

This allows, for instance, form-A to subscribe to an event on form-B, but still allow form-A to be garbage collected if it were closed first. Normally, if form-A were closed, it would still hang around in memory because the fact that it's subscribed to an event means there's still a reference to it hanging around.

Really, I would argue that most or all event should be weak references.

BlueRaja - Danny Pflughoeft
  • 80,074
  • 30
  • 187
  • 269
0

The MSDN description is fairly informative and includes a hypothetical example.

Ryan Emerle
  • 15,060
  • 8
  • 49
  • 67