C # basic series-Class 3 (destructor)

Source: Internet
Author: User

The Destructor is not only used in C, but also in C #. It is easy to write.

~ Class Name (){}

Let's give you the sample code first ,. We will first create two projects-assemblylibrary (Class Library) and consoltest (console)

Assemblylibrary

 public class DisposaClass //:IDisposable     {        public string a = "1111";        ~DisposaClass()        {            a = string.Empty;            Console.WriteLine("Press enter to Destroy it");          }}

Consoltest

  static void Main(string[] args)        {            DisposaClass dis = new DisposaClass();            //dis.Dispose();            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));            System.GC.Collect();            System.Threading.Thread.Sleep(10000);            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));            dis = null;            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));            Console.Read();         }

System. gc. Collect () This is the method for clearing the memory. gc. gettotalmemory (false) retrieves the number of bytes currently considered to be allocated

In the code, I also deliberately set the object to null and sleep the process. In theory, the object DIS should be completely released, and the console will print it out: press enter to destroy it?

Unfortunately, press enter to destroy it is not displayed. This is very simple. The system controls GC calls, and we can only issue commands to clear the memory, rather than force it to clean up the memory.

 

For some objects, as long as it can protected override void finalize, no ~ Class Name to implement the destructor, You can override finalize.

0: 32. No example is provided ~~~

 

If you say that the Destructor are called by the system, what do we want to do manually? If you want this, you can inherit the interface idisposable.

 public class DisposaClass :IDisposable     {        public string a = "1111";        ~DisposaClass()        {            a = string.Empty;            Console.WriteLine("Press enter to Destroy it");          }        public void Dispose()        {            a = string.Empty;        }    }

Idisposable and ~ The combination of class names can have a good effect on memory cleaning

Public class MyClass:IDisposable  {  private bool IsDisposed=false;  public void Dispose()  {     Dispose(true);     GC.SupressFinalize(this);  }  protected void Dispose(bool Diposing)  {     if(!IsDisposed)     {     if(Disposing)     {      //Clean Up managed resources     }     //Clean up unmanaged resources  }  IsDisposed=true;  }  ~MyClass()  {     Dispose(false);  }  }

  

I seldom use this method with people. Even though he can effectively control the memory, I used the order example and these examples when I was using PDA ~~ Because the memory is too small.

 

However, it takes some time to implement the idisposable interface. What if the customer cannot call them properly? For this reason, C # has a cool solution. 'Using' code block. It looks like this:

using (MyClass objCls =new MyClass())  {     }

This is also my favorite method, and I do not need to use my own dispose object. Especially when writing custom controls in winfrom, it is easy to overflow memory without dispose.

 

 

This chapter is really not powerful, and the harder it is to write these foundations.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.