L
Li jianzhong
First, look at the following code:
using System;
public class Grandpapa
{
~Grandpapa(){ Console.WriteLine
("Grandpapa.~Grandpapa");}
}
public class Parent:Grandpapa
{
~Parent(){ Console.WriteLine("Parent.~Parent");}
}
public class Son
arent
{
~Son(){ Console.WriteLine("Son.~Son");}
}
public class App
{
public static void Main()
{
Son s=new Son();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
The above code's output is :
Son.~Son
Parent.~Parent
Grandpapa.~Grandpapa
OK, no problem. But let's change the class Parent's
definition as fowllowing:
public class Parent:Grandpapa
{
protected void Finalize(){ Console.WriteLine
("Parent.Finalize");}
}
The output will be:
Son.~Son
Parent.Finalize
Very odd, right? Here is an odder one, change the class
Parent's definition as fowllowing:
public class Parent:Grandpapa
{
protected virtual void Finalize(){ Console.WriteLine
("Parent.Finalize");}
}
The output will be:
Grandpapa.~Grandpapa
Could you C# guys in Microsoft explain the odd output?
BTW, the output is same in .NET Framework 1.0/1.1/2.0
(Longhorn alpha version)?
using System;
public class Grandpapa
{
~Grandpapa(){ Console.WriteLine
("Grandpapa.~Grandpapa");}
}
public class Parent:Grandpapa
{
~Parent(){ Console.WriteLine("Parent.~Parent");}
}
public class Son

{
~Son(){ Console.WriteLine("Son.~Son");}
}
public class App
{
public static void Main()
{
Son s=new Son();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
The above code's output is :
Son.~Son
Parent.~Parent
Grandpapa.~Grandpapa
OK, no problem. But let's change the class Parent's
definition as fowllowing:
public class Parent:Grandpapa
{
protected void Finalize(){ Console.WriteLine
("Parent.Finalize");}
}
The output will be:
Son.~Son
Parent.Finalize
Very odd, right? Here is an odder one, change the class
Parent's definition as fowllowing:
public class Parent:Grandpapa
{
protected virtual void Finalize(){ Console.WriteLine
("Parent.Finalize");}
}
The output will be:
Grandpapa.~Grandpapa
Could you C# guys in Microsoft explain the odd output?
BTW, the output is same in .NET Framework 1.0/1.1/2.0
(Longhorn alpha version)?