-1

I'm dealing with an C# windows form application and I need to run class destructor immediately right after destroying my object .

using System;
namespace Test
{
    class TTTest
    {
        public TTTest()
        {
            Console.WriteLine("Constructor Called");
        }

        ~TTTest()
        {
            Console.WriteLine("Destroyed called");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            TTTest obj1 = new TTTest();
            TTTest obj2 = new TTTest();
            obj1 = null;
            obj2 = null;
            Console.ReadKey();
        }
    }
}

If I run this code ... The result will be :

Constructor Called
Constructor Called

and wont be :

Constructor Called
Constructor Called
Destroyed called
Destroyed called

I want to force compiler to run Garbage collector and I can do that by :

GC.Collect

But I want to run Destructor of each class right after destroying each object without calling GC.Collect() in my routines automatically.

Is there any way ?

I need a good way to destroyed any things right after destroying my object , and do not repeat GC.Collect() again and again .

Thanks for any help.

  • 3
    If you need to invoke a method on your terms, then you need to manage that. Finalizers are specifically "used to perform any necessary final clean-up when a class instance is being collected by the garbage collector." https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/finalizers - the finalizer is for the garbage collector, not for you, and that isn't changing. Why ever you think you need to do this, there's probably an alternate better solution to the underlying problem. – moreON Feb 14 '23 at 06:05
  • In my class , I create a temporary folder , and when I want to destroy my object , I want to delete this folder automatically , not by Garbage collector and not by calling a method , Just by Destroying object. If there is not any way , I do that by calling a method.Thank you for your time. – Mohsen Ilkhani Feb 14 '23 at 06:10
  • 2
    The usual solution to deterministic cleanup is to implement `IDisposable` and wrap usage of your class in `using(...)` statements. – Klaus Gütter Feb 14 '23 at 06:31
  • They are not called destructors because they are not the same thing. Unfortunately, they have the same syntax, but the thing is there is nothing in C# that behaves like destructors. The closest thing is `IDisposable` which could be used to do what you're asking for. – Brian Rasmussen Feb 14 '23 at 06:39
  • 1
    Beginners from another programming language should carefully study the GC based programming paradigm and get used to the proper terms like "finalizer". The more you stick to the old style such as "destructor", the further away you are from the right path. In the linked thread, Eric Lippert left important hints on how different finalizers are, which you might keep in mind. – Lex Li Feb 14 '23 at 06:41
  • @BrianRasmussen To be fair, [they actually _were_](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/finalizers) : _"Finalizers **(historically referred to as destructors)** are used to ..."_ (emphasis by me) – Fildor Feb 14 '23 at 07:40
  • @Fildor sure, but at least they changed the name in an attempt to reduce the confusion. It was a bad idea to borrow the name and the syntax when the behavior is completely different. – Brian Rasmussen Feb 14 '23 at 08:51
  • @BrianRasmussen Absolutely agreed. – Fildor Feb 14 '23 at 08:56

1 Answers1

1

In c# you would implement such a mechanic using Dispose().

class TTTest : IDisposable
{
    public TTTest()
    {
        Console.WriteLine("Constructor Called");
    }

    public void Dispose()
    {
        Console.WriteLine("Dispose called");
    }
}

class Program
{
    static void Main(string[] args)
    {
        using (TTTest obj1 = new TTTest())
        using (TTTest obj2 = new TTTest())
        {
            Console.ReadKey();
        }
    }
}
John Wu
  • 50,556
  • 8
  • 44
  • 80