help
Root Question Message
var we = Method1();
Console.WriteLine(IsGcCollected(we));
Method2();
[MethodImpl(MethodImplOptions.NoInlining)]
WeakReference Method1()
{
var c = new Class1(1);
return new WeakReference(c);
}
[MethodImpl(MethodImplOptions.NoInlining)]
void Method2()
{
var c = new Class1(1);
var w = new WeakReference(c);
c = null;
Console.Write(IsGcCollected(w));
}
bool IsGcCollected(WeakReference w)
{
for (var i = 0; w.IsAlive && i < 10; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
return !w.IsAlive;
}
class Class1{
public Class1(int propterty1)
{
Propterty1 = propterty1;
}
public int Propterty1 { get; set; }
}