C # constructor and destructor

Source: Internet
Author: User

Constructor and destructor relay http://www.cnblogs.com/philzhou/archive/2010/08/10/CSharp_Constructor.html

This section describes how to create, initialize, and destroy an object. This section describes the following topics:

L class Constructor

L structure Constructor

L destructor

Class Constructor

This section describes three types of constructor:

Class constructor type

Note

Instance

The instance used to create and initialize the class.

Private

Special instance constructors that are not accessible outside of the class. Classes cannot be instantiated using private constructors.

Static

This constructor is automatically called to initialize the class before the first instance is created or any static member is referenced. You cannot directly call this constructor.

Remarks

(1) instance Constructor

Class constructor is called when a new object is created, for example:

Point mypoint = new point ();

A class can have multiple constructors. For example, you can declare a constructor without parameters (for example
Point () and a constructor with parameters (such as point (int x, int y )).

If the class has no constructor, a default non-parameter constructor is automatically generated and the default value is used to initialize the object field (for example, int is initialized to 0 ).

Class constructor can call the base class constructor by using the base keyword. For example:

Public cylinder (double radius, double height): Base (radius, height)

{

}

The class constructor can also call another constructor of the same class by using the keyword "this". For example:

Public point (): This (0, 20)

{

}

In the previous example, no parameter is set to point ()
Another constructor with two parameters is called to initialize the default position to (0, 20 ).

(2)Private Constructor

A private constructor is a special instance constructor. It is usually used in a class that only contains static members. If a class has one or more private constructors without a public constructor, other classes (except Nested classes) are not allowed to create instances of the class. For example:

Class nlog

{

// Private constructor:

Private nlog (){}

Publicstatic Double E = 2.71828;

}

Declare an empty constructor to Prevent Automatic Generation of default constructor. Note: if you do not use an access modifier for the constructor, it is still a private constructor by default. However, the private modifier is explicitly used to explicitly indicate that the class cannot be instantiated.

(3)Static Constructor

Static constructors are used to initialize classes. Before creating the first instance or referencing any static member, the class is automatically initialized by calling the static constructor.

The static constructor neither has an access modifier nor a parameter.

Before creating the first instance or referencing any static member (method or field), the static constructor is automatically called to initialize the class.

Static constructor cannot be called directly. The static constructor cannot access non-static members. It is called only once.

In a program, you cannot control when to execute static constructors.

A typical use of a static constructor is to use this constructor to write entries to a log file when the class uses a log file.

Structure Constructor

The structure constructor is similar to the class constructor, but there are the following differences:

The N structure cannot contain explicit non-parameter constructors. Structure members are automatically initialized to their default values.

The N structure cannot have any of the following initial values: Base (argument-list ).

The structure can declare constructors, but they must contain parameters. The default (No parameter) constructor of the declared structure is incorrect. The structure member cannot have an initial value. The default constructor is always provided to initialize the structure members as their default values.

For structures, there is no inheritance like classes. A structure cannot inherit from another structure or class. The structure cannot have any of the following initial values: Base (argument-list ).

Destructor

Destructor are used to destroy class instances.

Remarks

You cannot use destructor for structures. Only destructor can be used for classes.

A class can have only one destructor.

The Destructor cannot be inherited or overloaded.

You cannot call the destructor. They are automatically called.

The Destructor neither have modifiers nor parameters. For example, the declaration of the destructor of the class myclass is as follows:

~ Myclass ()

{

// Cleanup statements.

}

The Destructor implicitly calls the object. Finalize method to the base class of the object. In this way, the previous destructor code is implicitly converted:

Protected override void finalize ()

{

Try

{

// Cleanup statements.

}

Finally

{

Base. Finalize ();

}

}

This means that the Finalize method is called recursively for all instances in the inheritance chain (from the largest degree of similarity to the least degree of similarity derived.

Programmers cannot control when to call the Destructor because it is determined by the garbage collector. The garbage collector checks whether there are objects that are no longer used by the application. It considers that these objects meet the destruction conditions and reclaim the memory occupied by these objects. The Destructor is also called when the program exits.

BaseKeywords

The base keyword is used to access the members of the base class from the derived class:

Call methods that have been overwritten or hidden by other methods on the base class.

Specify the base class constructor to call when creating a derived class instance.

Base Class access can only be performed in constructors, instance methods, or instance attribute accessors.

It is incorrect to use the base keyword in static methods.

ThisKeywords

This keyword indicates the current instance of the class.

The static member does not have the this pointer.

Access members hidden by the same name.

For example:

Public Employee (string name, string alias)

{

This. Name = Name;

This. Alias = alias;

}

Pass this as a parameter to other methods.

For example:

Calctax (this );

Declare the indexer, for example:

Public int this [int Param]

{

Get

{

Return array [Param];

}

Set

{

Array [Param] = value;

}

}

Call other constructors of the same type

Static Method and static property accessors reference this
Yes.

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.