I know that Destructors in c# do not have an execution order.
The following Structure i do use in several Classes, it's to Desturct instances and the Static Information:
public class MyClass
{
private static readonly Destructor DestructorObject = new Destructor();
~MyClass()
{
Console.WriteLine("Destructor Called");
}
static void Main(string[] args)
{
var myClass = new MyClass();
}
private sealed class Destructor
{
~Destructor()
{
Console.WriteLine("Static Destructor Called");
}
}
}
As I mentioned above the order of Destuctors is undifined. But as im using this construct in many classes I found out, that in each class there is a non changing order, which also remains even if I'm recompiling the application and running it again.
Means that a MyClass1
could alaways run ~MyClass1
first and another class MyClass2
could alawways run ~Destructor
first.
As there clearly is a "hidden" order for each class, can I trust it?