CSharpTest.Net
WeakReference<T> Constructor(T)
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Utils Namespace > WeakReference<T> Class > WeakReference<T> Constructor : WeakReference<T> Constructor(T)

instance

Glossary Item Box

Creates a new WeakReference that keeps track of target.

Syntax

Visual Basic (Declaration) 
Public Function New( _
   ByVal instance As T _
)
C# 
public WeakReference<T>( 
   T instance
)

Parameters

instance

Example

Library/Library.Test/TestWeakReferenceT.cs

C#Copy Code
string value1 = "Testing Value - 1";
object value2 = new MyObject();
Utils.WeakReference<string> r = new Utils.WeakReference<string>(value1);

string tmp;
Assert.IsTrue(r.TryGetTarget(out tmp) && tmp == value1);

((WeakReference)r).Target = value2; //incorrect type...
Assert.IsFalse(r.IsAlive);
Assert.IsNull(r.Target);
Assert.IsFalse(r.TryGetTarget(out tmp));

Assert.IsTrue(ReferenceEquals(value2, ((WeakReference)r).Target));
VB.NETCopy Code
Dim value1 As String = "Testing Value - 1"
Dim value2 As Object = New MyObject()
Dim r As New Utils.WeakReference(Of String)(value1)

Dim tmp As String
Assert.IsTrue(r.TryGetTarget(tmp) AndAlso tmp = value1)

(DirectCast(r, WeakReference)).Target = value2
'incorrect type...
Assert.IsFalse(r.IsAlive)
Assert.IsNull(r.Target)
Assert.IsFalse(r.TryGetTarget(tmp))

Assert.IsTrue(ReferenceEquals(value2, (DirectCast(r, WeakReference)).Target))

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys