Export (0) Print
Expand All

WeakReference Class

Represents a weak reference, which references an object while still allowing that object to be reclaimed by garbage collection.

System.Object
  System.WeakReference

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[<SerializableAttribute>]
[<ComVisibleAttribute(true)>]
[<SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode)>]
type WeakReference =  
    class
        interface ISerializable
    end

The WeakReference type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryWeakReference(Object)Initializes a new instance of the WeakReference class, referencing the specified object.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryWeakReference(Object, Boolean)Initializes a new instance of the WeakReference class, referencing the specified object and using the specified resurrection tracking.
Protected methodWeakReference(SerializationInfo, StreamingContext)Initializes a new instance of the WeakReference class, using deserialized data from the specified serialization and stream objects.
Top

  NameDescription
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryIsAliveGets an indication whether the object referenced by the current WeakReference object has been garbage collected.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryTargetGets or sets the object (the target) referenced by the current WeakReference object.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryTrackResurrectionGets an indication whether the object referenced by the current WeakReference object is tracked after it is finalized.
Top

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryFinalizeDiscards the reference to the target represented by the current WeakReference object. (Overrides Object.Finalize().)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetObjectDataPopulates a SerializationInfo object with all the data needed to serialize the current WeakReference object.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)
Top

A weak reference allows the garbage collector to collect an object while still allowing an application to access the object. If you need the object, you can still obtain a strong reference to it and prevent it from being collected. For more information about how to use short and long weak references, see Weak References.

The following example demonstrates how you can use weak references to maintain a cache of objects as a resource for an application. The cache is constructed using an IDictionary(TKey, TValue) of WeakReference objects keyed by an index value. The Target property for the WeakReference objects is an object in a byte array that represents data.

The example randomly accesses objects in the cache. If an object is reclaimed for garbage collection, a new data object is regenerated; otherwise, the object is available to access because of the weak reference.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft