Odd constructor

What does ~. This is the Asp.Net v2 class that I just inherited. The class is working fine. Creates a default constructor below it ~ VNC What is used ~?

using System; using System.Web; using System.Data; using System.Web.Services; using System.Web.Services.Protocols; using System.Data.SqlClient; using System.Configuration; namespace UtopianClasses { public class VNC { public VNC(){ } ~VNC() { GC.Collect(); } public String VNCViewFilesLocationMCR { get { return ConfigurationManager.AppSettings["VNCViewFilesLocation"].ToString(); } 

... the rest of the class

+4
source share
3 answers

As already mentioned, this is a destructor. However, you have to explicitly implement a destructor, you really should not make GC collection from within the destructor, because the destructor is called by the garbage collector. As far as I can tell, the smell of code is in any GCed language.

+3
source

This is a destructor , not a constructor. They are commonly used to release their own resources or descriptors in classes that wrap their own APIs.

Saying adding a constructor that calls GC.Collect() is a bad idea. The destructor is called only during finalization, so the garbage collector already clears this object and any object referenced by this object. There is no reason for this, since it simply adds extra overhead and actually slows down the system.

This was probably written by someone with a C ++ background that did not understand the intricacies of memory management in .NET. I would recommend deleting this at all.

+1
source

They are actually called finalizers in .net. They are called when the GC clears this object.

http://msdn.microsoft.com/en-us/library/system.object.finalize(VS.71).aspx

0
source

Source: https://habr.com/ru/post/1303312/


All Articles