Ask Learn
Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
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. |
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)
The object to track or null
.
This constructor creates a short weak reference to target
.
Product | Versions |
---|---|
.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 |
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)
An object to track.
Indicates when to stop tracking the object. If true
, the object is tracked after finalization; if false
, the object is only tracked until finalization.
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
If trackResurrection
is false
, a short weak reference is created. If trackResurrection
is true
, a long weak reference is created.
Product | Versions |
---|---|
.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 |
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);
[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);
[<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
new WeakReference : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> WeakReference
Protected Sub New (info As SerializationInfo, context As StreamingContext)
An object that holds all the data needed to serialize or deserialize the current WeakReference object.
(Reserved) Describes the source and destination of the serialized stream specified by info
.
info
is null
.
The context
parameter is reserved, and does not currently participate in this operation.
Product | Versions (Obsolete) |
---|---|
.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 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Please sign in to use this experience.
Sign in