Code Description:
Code
Class Program
{
Static void main (string [] ARGs)
{
Dowork ();
Console. writeline (P. Target = NULL );
GC. Collect ();
Console. writeline (P. Target = NULL );
Console. Readline ();
}
Public static weakreference P;
Public static void dowork ()
{
VaR P = getperson ();
P = new weakreference (P );
}
Public static person getperson ()
{
Return new person ();
}
}
Public class person: idisposable
{
Public Person ()
{
For (INT I = 0; I <new random (). Next (18, 80); I ++)
{
This. memo. append (char) new random (). Next (97,123 ));
}
}
Public stringbuilder memo {Get; set ;}
Public bool isdisposed {Get; set ;}
# Region idisposable Member
Public void dispose ()
{
If (! This. isdisposed)
{
Console. writeline ("person dispose ...");
This. memo. Remove (0, this. memo. Length );
This. isdisposed = true;
GC. suppressfinalize (this );
}
}
# Endregion
~ Person ()
{
Console. writeline ("person unctor ...");
This. Dispose ();
}
}