class Person {
public string Name { get; set; }
public Person(string Name) {
Console.WriteLine($"Instantiate {Name}");
this.Name = Name;
}
~Person() {
Console.WriteLine($"{Name}'s Finalizer fired");
}
}
class Program
{
static void Main(string[] args)
{
new Person("Pawel");
Console.WriteLine("press key to garbage
collect");
Console.ReadKey();
GC.Collect();
Console.ReadKey();
}
}
the finalizer is used to release unmanaged resources that are being used by this class. the base object class has no implementation so if you don't override it, it'll never fire. if you do overwrite it the garbage collector puts it into a finalization queue.
to read more about the msdn finalizer