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


Jump to: navigation, search
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


Personal tools