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,515
  • 2
  • 16
  • 19

3 Answers3

1

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

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
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
  • 84,206
  • 33
  • 197
  • 283
0

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

Ryan Emerle
  • 15,461
  • 8
  • 52
  • 69