none
Destructor in C# RRS feed

  • Question

  • As far as I know, the destructor is called implicitly by the .NET Framework's Garbage collector and therefore programmer has no control as when to invoke the destructor.

    However, if I search Finalizer from MSDN, there's still destructor exists(for example, ~Car()).

    When can I use destructor?

    Friday, January 17, 2020 9:10 PM

Answers

  • Hello,

    See the following length blog post.

    https://ericlippert.com/2015/05/18/when-everything-you-know-is-wrong-part-one/


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Jeff0803 Monday, January 20, 2020 3:04 AM
    Friday, January 17, 2020 10:06 PM
    Moderator
  • When can I use destructor?

    You would only use it when you need to free some non-managed resources that were allocated within your class. Managed resources get deallocated automatically by the Framework.

    However, you should be aware that the destructor does not behave as you would expect if you have coded in other languages such as C++. The destructor is not called immediately; instead, when your object becomes unreachable, it is added to the destruction queue. A separate thread executes the destructors of all the objects in the destruction queue, and only after the destructor has been executed the object is made available to the Garbage Collector and it can be finally freed when the GC makes a collection pass.

    • Marked as answer by Jeff0803 Monday, January 20, 2020 3:04 AM
    Saturday, January 18, 2020 11:14 AM
    Moderator

All replies

  • Hello,

    See the following length blog post.

    https://ericlippert.com/2015/05/18/when-everything-you-know-is-wrong-part-one/


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Jeff0803 Monday, January 20, 2020 3:04 AM
    Friday, January 17, 2020 10:06 PM
    Moderator
  • When can I use destructor?

    You would only use it when you need to free some non-managed resources that were allocated within your class. Managed resources get deallocated automatically by the Framework.

    However, you should be aware that the destructor does not behave as you would expect if you have coded in other languages such as C++. The destructor is not called immediately; instead, when your object becomes unreachable, it is added to the destruction queue. A separate thread executes the destructors of all the objects in the destruction queue, and only after the destructor has been executed the object is made available to the Garbage Collector and it can be finally freed when the GC makes a collection pass.

    • Marked as answer by Jeff0803 Monday, January 20, 2020 3:04 AM
    Saturday, January 18, 2020 11:14 AM
    Moderator