WeakReference Constructors

Definition

Initializes a new instance of the WeakReference class.

Overloads

WeakReference(Object)

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

WeakReference(Object, Boolean)

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

WeakReference(SerializationInfo, StreamingContext)
Obsolete.

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

WeakReference(Object)

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

public:
 WeakReference(System::Object ^ target);
public WeakReference (object target);
public WeakReference (object? target);
new WeakReference : obj -> WeakReference
Public Sub New (target As Object)

Parameters

target
Object

The object to track or null.

Remarks

This constructor creates a short weak reference to target.

See also

Applies to

WeakReference(Object, Boolean)

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

public:
 WeakReference(System::Object ^ target, bool trackResurrection);
public WeakReference (object target, bool trackResurrection);
public WeakReference (object? target, bool trackResurrection);
new WeakReference : obj * bool -> WeakReference
Public Sub New (target As Object, trackResurrection As Boolean)

Parameters

target
Object

An object to track.

trackResurrection
Boolean

Indicates when to stop tracking the object. If true, the object is tracked after finalization; if false, the object is only tracked until finalization.

Examples

The following example creates a cache of data objects with short weak references. This example is part of a larger example provided for the WeakReference class.

 // Add objects with a short weak reference to the cache.
for (int i = 0; i < count; i++) {
     _cache.Add(i, new WeakReference(new Data(i), false));
 }
// Add objects with a short weak reference to the cache.
for i = 0 to count - 1 do
    _cache.Add(i, WeakReference(Data i, false))
' Add data objects with a short weak reference to the cache. 
For ctr = 0 To count - 1
    _cache.Add(ctr, New WeakReference(New Data(ctr)))
Next

Remarks

If trackResurrection is false, a short weak reference is created. If trackResurrection is true, a long weak reference is created.

See also

Applies to

WeakReference(SerializationInfo, StreamingContext)

Caution

This API supports obsolete formatter-based serialization. It should not be called or extended by application code.

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

protected:
 WeakReference(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
protected WeakReference (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected WeakReference (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
new WeakReference : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> WeakReference
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new WeakReference : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> WeakReference
Protected Sub New (info As SerializationInfo, context As StreamingContext)

Parameters

info
SerializationInfo

An object that holds all the data needed to serialize or deserialize the current WeakReference object.

context
StreamingContext

(Reserved) Describes the source and destination of the serialized stream specified by info.

Attributes

Exceptions

info is null.

Remarks

The context parameter is reserved, and does not currently participate in this operation.

Applies to