The difference between a destructor and a finalizer?

后端 未结 4 2162
迷失自我
迷失自我 2020-12-08 19:09

Please Note: This question is about the difference in terminology between the words \"destructor\" and \"finalizer\" and their correct usage. I have merely provided

4条回答
  •  青春惊慌失措
    2020-12-08 19:29

    I believe 'destructor' is the C# code, and the 'finalizer' is the compiled CIL method. The C# compiler turns a destructor into a finalizer.

    EDIT: To be more verbose; The C# language specification defines a 'destructor' as a C# instance method on a class. 'destructor', then, is part of the C# grammar -- destructors are a linguistic object appearing in source code.

    A destructor is a member that implements the actions required to destruct an instance of a class. Destructors cannot have parameters, they cannot have accessibility modifiers, and they cannot be invoked explicitly. The destructor for an instance is invoked automatically during garbage collection.

    A 'finalizer' is a term used in the Common Language Runtime, for example in calls to GC.WaitForPendingFinalizers(), and it refers to any .net language, not just C#. Any .net reference type can have a finalizer. For a C# class (the source code artifact), the destructor compiles into the CIL method which is the CLR type's finalizer.

    Or more succinctly, destructor is to finalizer as source code is to machine code ;)

提交回复
热议问题