C# FAQ: What are the differences between CSharp finalizers and Java destructors
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:FAQs |
| edit |
What are the differences between C# finalizers and Java destructors?
C# features hybrid destructors which use a C++ style, destructor syntax yet share most of their semantics with Java finalizers.
The following C# and Java examples are equivalent:
using System; public class CSharpClass { ~CSharpClass() { } }
public class JavaClass { public void finalize() { } }
Although C# destructors automatically call the base class destructor after execution, this does not happen in the Java finalizer system.
Visual C# Best Practice
Finalizers are used carefully by experienced developers; because, the order of finalization cannot be controlled: Objects which hold references to each other may need to be finalized in a certain order. Further, finalization involves significant overhead; because, objects with finalizers are garbage collected after the finalizer thread ends rather than after the garbage collection thread. Thus, such objects live longer in the system.
See also
- Are C# constructors inherited?
- How can one constructor call another?
- Are constructors the same in C++ and C#?
- Are destructors the same in C++ and C#?
- How call a virtual method from a constructor or destructor?
- How do destructors work in C#?
- How enforce constructor correctness in C#?
- How make a C# destructor virtual?
- What are the differences between C# and Java constructors?
- What are the differences between C# finalizers and Java destructors?
- What is the syntax for calling an overloaded constructor from within a constructor?
- Why must
structconstructors have at least one argument?