(original) C # learning note 08--Introduction to Object-oriented programming 01--the meaning of object-oriented programming 03--the life cycle of objects--constructors and destructors

Source: Internet
Author: User

8.1.3 The life cycle of an object

Each object has a well-defined life cycle, and there are two important phases in addition to the normal state of "in use":

Construction phase: The period at which an object is initially instantiated. This initialization process is called the construction phase, which is done by the constructor.

destructor stage: When deleting an object, it is often necessary to do some cleanup work, such as freeing up memory, which is done by the destructor.

1. Constructors

The initialization process for an object is done automatically. We don't need to find a memory space that is suitable for storing new objects. However, in the process of initializing an object, some additional work is sometimes required. For example, you need to initialize the data stored by the object. constructors are functions that are used to initialize data .

All class definitions contain at least one constructor. In these constructors, there may be a default constructor, which has no arguments and has the same name as the class. A class definition may also contain several constructors with parameters, called non-default constructors. Code can use them to instantiate objects in many ways, such as providing an initial value to the data stored in the object.

In C #, the constructor is called with the New keyword. For example, you can instantiate a Cupofcoffee object by its default constructor in the following way:

New

You can also create an object with a non-default constructor. For example, the Cupofcoffee class has a non-default constructor that uses a parameter to set the brand of the coffee bean when it is initialized:

New Cupofcoffee ("Blue Mountain"

constructors, like fields, properties, and methods, can be public or private. code outside the class cannot instantiate an object using a private constructor, but must use a public constructor . This allows the user of the class to use a non-default constructor (set the default constructor to private).

  Some classes do not have public constructors, and external code cannot instantiate them, which are called non-created classes , but these classes are not completely useless as described later.

2. Destructors

In general, you do not need to provide the code for the destructor, but instead the default destructor performs the operation automatically. However, if you need to do some important things before you delete an object instance, you should provide a specific destructor.

For example, if a variable is out of range, the code cannot access it, but the variable still exists somewhere in the computer's memory. Only in. NET runtime to perform its garbage collection, the instance is completely removed when it is cleaned up.

Note: You should not rely on destructors to release the resources used by an object instance because the resource is occupied by the object for a long time after it is no longer being used. This can be problematic if the resources used are important. There is a workaround, see the "objects can be deleted" section later in this chapter.

(original) C # learning note 08--Introduction to Object-oriented programming 01--the meaning of object-oriented programming 03--the life cycle of objects--constructors and destructors

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.