Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Garbage Collecting objects which keep track of their own instances in an internal Map
  • Create a weak reference to an object

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.

like image 227
Frank Krueger Avatar asked Dec 01 '22 12:12

Frank Krueger


1 Answers

Yes, there's a generic weak reference class.

MSDN > Weak Reference

like image 97
arul Avatar answered Dec 10 '22 01:12

arul