Search code examples
c#.netgarbage-collectionweak-references

Are there Weak References in .NET?


I would like to keep a list of a certain class of objects in my application. But I still want the object to be garbage collected. Can you create weak references in .NET?

For reference:

Answer From MSDN:

To establish a weak reference with an object, you create a WeakReference using the instance of the object to be tracked. You then set the Target property to that object and set the object to null. For a code example, see WeakReference in the class library.


Solution

  • Yes, there's a generic weak reference class.

    MSDN > Weak Reference