. The object life cycle of net component programming

Source: Internet
Author: User
Tags finally block

. The object life cycle of net component programming

    • . NET Garbage Collection
    • IDisposable ()
    • Using statement
. NET Garbage Collection

It is the CLR that manages the garbage collector, the garbage collector monitors the managed heap, and the objects we use, as well as the prerequisite object information that is required for system startup, exist on the managed heap, and the CLR maintains a list (the object reference Information list). The information stored in this list is the information that corresponds to all objects in the managed heap (references, referenced information) whenever a new object is used or a reference to an existing object is changed the CLR updates the list of object reference information. So when does the collector usually call?

Most of the garbage collector is triggered when the managed heap is exhausted, or it can be called through code (described below).

At the time of the garbage collector's invocation, it is traversed from (the object reference information list) to see if each object is reachable to the client (the application using these objects), and if so, the object is tagged to reach and skips it (to see if the object has a reference), and continues to traverse the And the object referenced by the tagged object will not be checked again. When the objects are not referenced by the object, the objects are reclaimed and spatially compressed, and the addresses that have the referenced objects are modified for spatial compression.

IDisposable ()

In. NET provides us with the IDispose interface is used to release resources, see a sample code:

1      Public InterfaceIMyInterface2     {3         voiddosomething ();4     }5      Public classstydyclass:imyinterface,idisposable6     {7          Public voidDispose ()8         {9             //Freeing ResourcesTen         } One          Public voiddosomething () A         { -             //Do something -         } the}

Look at the client calling code:

 1  imyinterface studyclass = new   Studyclass ();  2  studyclass.dosomething ();  3  as   IDisposable;  5  null   6  7   disposable. Dispose ();  8 } 

This is safe to call, and if Studyclass does not support the Idisposable,as operator, it will return a null value, which seems to be fine, but if the client object is shared, there are many objects that use it. Who exactly is to execute idisposable.dispose (). If there are some errors when the dosomething () function executes, will the resources be released?

using Statement

C # supports using statements that help us generate a try/finally block that uses the Dispose () method when using statements, and of course your object must support IDisposable first. Take a look at the sample code:

1 using New Studyclass ()) 2 {3    studyclass.dosomething (); 4 }

If you write this way, the compilation will not pass, and you will be prompted that the type used in the using statement must be implicitly convertible to System.IDisposable. Of course, there are ways to get such code compiled through, so that the IMyInterface interface is also derived from IDisposable. After that, you can write the code like this:

1IMyInterface Studyclass =NewStudyclass ();2 using(Studyclass asIDisposable)3 {4 studyclass.dosomething ();5 }6 7 using(Studyclass Studyclass =NewStudyclass ())8 {9 studyclass.dosomething ();Ten}

The above writing method can be achieved, the above two code is equal to the following code:

1Studyclass Studyclass =NewStudyclass ();2 Try3 {4 studyclass.dosomething ();5 }6 finally7 {8    if(Studyclass! =NULL)9     {TenIDisposable dis =Studyclass; One Dis. Dispose (); A     } -}

The content of this article is not very complicated and profound, but the basic explanation of the object's life cycle management, there are some in-depth knowledge is not to explain, and some of us in the object-oriented design, ready for some important high-usage objects for the abstract design, As far as possible to let the abstract object to implement the IDisposable interface, so there is no error message in the above content.


Source: http://www.cnblogs.com/jin-yuan/

This article is copyrighted by the author and the blog Park, welcome reprint, but without the consent of the author must retain this statement, and on the article page

Related Article

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.