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(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 a hash function for a particular type. (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.