Ler em inglês Editar

Compartilhar via


WeakReference Constructors

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

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)

Source:
WeakReference.cs
Source:
WeakReference.cs
Source:
WeakReference.cs

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

public WeakReference(object target);
public WeakReference(object? target);

Parameters

target
Object

The object to track or null.

Remarks

This constructor creates a short weak reference to target.

See also

Applies to

.NET 10 e outras versões
Produto Versões
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

WeakReference(Object, Boolean)

Source:
WeakReference.cs
Source:
WeakReference.cs
Source:
WeakReference.cs

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

public WeakReference(object target, bool trackResurrection);
public WeakReference(object? target, bool trackResurrection);

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));
 }

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

.NET 10 e outras versões
Produto Versões
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

WeakReference(SerializationInfo, StreamingContext)

Source:
WeakReference.cs
Source:
WeakReference.cs
Source:
WeakReference.cs

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.

[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);
protected WeakReference(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);

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

.NET 10 e outras versões
Produto Versões (Obsoleto)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7 (8, 9, 10)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1