Represents a weak reference, which references an object while still allowing that object to be reclaimed by garbage collection.
Assembly: mscorlib (in mscorlib.dll)
Thetype exposes the following members.
|WeakReference()||Initializes a new instance of the class. This constructor overload cannot be implemented in XAML-based applications.|
|WeakReference(Object)||Initializes a new instance of the class, referencing the specified object.|
|WeakReference(Object, Boolean)||Initializes a new instance of the class, referencing the specified object and using the specified resurrection tracking.|
|WeakReference(SerializationInfo, StreamingContext)||Initializes a new instance of the class, using deserialized data from the specified serialization and stream objects.|
|IsAlive||Gets an indication whether the object referenced by the current object has been garbage collected.|
|Target||Gets or sets the object (the target) referenced by the current object.|
|TrackResurrection||Gets an indication whether the object referenced by the current object is tracked after it is finalized.|
|Equals(Object)||Determines whether the specified object is equal to the current object. (Inherited from Object.)|
|Finalize||Discards the reference to the target represented by the current object. (Overrides Object.Finalize().)|
|GetHashCode||Serves as the default hash function. (Inherited from Object.)|
|GetObjectData||Populates a SerializationInfo object with all the data needed to serialize the current object.|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|ToString||Returns a string that represents the current object. (Inherited from Object.)|
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 objects keyed by an index value. The Target property for the 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.
.NET FrameworkSupported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
Portable Class LibrarySupported in: Portable Class Library
.NET for Windows Store appsSupported in: Windows 8
Supported in: Windows Phone 8.1
Supported in: Windows Phone Silverlight 8.1
Supported in: Windows Phone Silverlight 8
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)