Possible Duplicate:
Weak references
I understand the concept of a weak reference, but I am unable to find where I should use a weak reference in C#.
Possible Duplicate:
Weak references
I understand the concept of a weak reference, but I am unable to find where I should use a weak reference in C#.
A good example of where to use a WeakReference would be when implementing the EventAggregator pattern.
Say you have the code
eventAggregator.Subscribe<AnEventType>(this.DoSomethingDelegate);
then you will specifically ahve to unsubscribe later if you don't want to have a potential memory leak. See Explicitly Removing Event Handlers for more info.
If however the internals of the EventAggregator hold on to the DoSomethingDelegate using a weak reference, then no unsubscription is necessary.
For further learning, I suggest taking a look at the implementation of EventAggregator in the Microsoft Practices library using ILSpy. This internally uses a WeakReferenceDelegate type which wraps a Weakdelegate and allows subscription without explicit unsubscription and no chance of a memory leak.
Best regards,