WeakReference Class

 

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

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

System::Object
  System::WeakReference

[SerializableAttribute]
[ComVisibleAttribute(true)]
[SecurityPermissionAttribute(SecurityAction::InheritanceDemand, 
	Flags = SecurityPermissionFlag::UnmanagedCode)]
public ref class WeakReference : ISerializable

NameDescription
System_CAPS_pubmethodWeakReference(Object^)

Initializes a new instance of the WeakReference class, referencing the specified object.

System_CAPS_pubmethodWeakReference(Object^, Boolean)

Initializes a new instance of the WeakReference class, referencing the specified object and using the specified resurrection tracking.

System_CAPS_protmethodWeakReference(SerializationInfo^, StreamingContext)

Initializes a new instance of the WeakReference class, using deserialized data from the specified serialization and stream objects.

NameDescription
System_CAPS_pubpropertyIsAlive

Gets an indication whether the object referenced by the current WeakReference object has been garbage collected.

System_CAPS_pubpropertyTarget

Gets or sets the object (the target) referenced by the current WeakReference object.

System_CAPS_pubpropertyTrackResurrection

Gets an indication whether the object referenced by the current WeakReference object is tracked after it is finalized.

NameDescription
System_CAPS_pubmethodEquals(Object^)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Discards the reference to the target represented by the current WeakReference object.(Overrides Object::Finalize().)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetObjectData(SerializationInfo^, StreamingContext)

Populates a SerializationInfo object with all the data needed to serialize the current WeakReference object.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodToString()

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 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.

SecurityPermission

for the ability to call unmanaged code. Demand value: InheritanceDemand; Permission value: UnmanagedCode

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

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

Return to top
Show: