Name

WeakReference

Synopsis

This class encapsulates a weak reference to an object. By default, when you instantiate a .NET class, you create a strong reference, which prevents the garbage collector from removing the object and reclaiming memory. A weak reference, however, does not prevent an object from being released.

Objects that are weakly referenced can still be kept alive as long as there is at least one strong reference to them. That means a weak reference allows you to access an object as long as it is in use by another part of your application. For example, objects can be stored in a collection using a weak reference, but not kept alive just because they are in the collection.

To create a weakly referenced object, pass the name of the object to the WeakReference constructor. You can use the IsAlive property to check if the reference is valid, and the Target property to get a reference to the actual object. Assigning the Target property to another variable creates a strong reference.

You can set the TrackResurrection property to true in the constructor to maintain a long weak reference, which tracks an object during (or after) finalization.

Public Class WeakReference : Implements System.Runtime.Serialization.ISerializable
' Public Constructors
   Public Sub New( ByVal target As Object) 
   Public Sub New(ByVal target As Object, 
        ByVal trackResurrection As Boolean) 
' Protected Constructors
   Protected Sub New( ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context ...

Get VB.NET Core Classes in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.