Collection of object-intensive resources in. Net

Source: Internet
Author: User




one, using the Finalizer destructor


1, how are objects that use the finalizer destructor recycled?


The object that defines the destructor in the class will be moved to a dedicated queue, which will act as its application root, leaving the objects in the queue to survive longer, and after the destructor is called on the object, the object will be cleaned out of the queue.


2, using the example

<span style= "Font-family:microsoft Yahei;" > class program    {        static void Main (string[] args)        {            cup cup = new Cup () {                             cupname= "LHC's small cup ~ ~ ~" c5/>};        }    }    public class Cups {public        string Cupname {get; set;}        ~cup () {            //place the destructor code here: Compare the destructor of C + +, feel the meaning is the same                //place the code that frees the resource            Console.WriteLine ("The small cup has been broken ~ ~ ~ ~~~~~");               }        }</span>


It feels like C + + in grammar, hehe ~



3, what are the issues that should be noticed with the destructor?

1 , development This does not know exactly when the destructor will be called

2 , the destructor lengthens the object's survival time

3 , do not write blocking methods or time-consuming methods in destructors, the destructor should be a quick release of resources and end

4 , a garbage collection is performed when the program exits and the destructor is called if the program has not been garbage collected during the run



Two, implement IDisposable interface

Finalizer the execution time is indeterminate, and sometimes we expect the client object to be freed immediately after it is finished, which can be achieved IDispose interface.


Try the next interface in dispose of what will happen, feel or similar C + +, write a chestnut to observe what happens when the destruction.


Examples of Use:


<span style= "Font-family:microsoft Yahei;"    > static void Main (string[] args) {#region constructor using//cup cup = new Cup () {//             Cupname= "LHC's small cup ~ ~ ~"//};                        #endregion #region Use of dispose bigcup Bigcup = new Bigcup () {                Cupname = "Small cup of the LHC"};                Try//{//Bigcup.cupname = "Simulate the use of objects here";                }//finally//{//IDisposable dis = Bigcup as IDisposable; if (dis! = null)//{//DIS.                Dispose ();  }//} using (Bigcup as IDisposable) {Bigcup.cupname                = "Simulate the use of objects here"; } #endregion}} public class Bigcup:idisposable {public string Cupname {get; set;} void IDisposable.Dispose () {Console.WriteLine ("Destroyed ~ ~ ~ ~ ~ ~"); }}</span>



third, combining destructors and Dispose ()

The main problem with destructors is that it is not called immediately, but at an indeterminate time later, when garbage collection is performed. the Dispose() method also has its own problem, which is that the client does not necessarily call it. Therefore, the best way is to combine the two:

If the client calls the Dispose () method, then do not let CLR to perform destructors;

If the client is not called, then the destructor is made.


Use Demo together:

<span style= "Font-family:microsoft Yahei;"        > public class testfatherfinalizeranddispose:idisposable{private bool _dispose = false;        ~testfatherfinalizeranddispose () {Console.WriteLine ("Call destructor ~ ~ ~ ~ ~ ~ ~");                public void IDisposable.Dispose () {if (!_dispose) {_dispose = true;                CleanUp (); Gc.            SuppressFinalize (this);//indicates the garbage collection period, ignoring the destructor of the object. }}//Subclasses can override the Dispose resource method public virtual void CleanUp () {//Place cleanup resource code here Consol        E.writeline ("Garbage removal is complete");        }} public class Testsonfinalizeranddispose:testfatherfinalizeranddispose {public override void CleanUp ()            {try {Console.WriteLine ("Fill out the method of releasing the subtype resources here"); } finally {base.  CleanUp (); Call the parent class's method to release the parent class's Resource}}}</span>


Perfect Solution ~ ~ ~ Continue to prepare operations research test, say review good boring, middle write a code solution monotony ~ ~









Collection of object-intensive resources in. Net

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.