The principle of constructor and secting functions and the use in C #
Summary: Constructor and destructor are two types of functions that seem simpler in a class, but will always have unexpected running errors during practical use. This article will compare the configuration of the system and the principle of the destructor and the application in C #, and several issues that need to be noted during the use process.
Keywords: constructor; destructor; garbage collector; non-hosting resources; managed resources
One. Principle of constructor and secting function
As a more advanced language than C, C # provides a better mechanism to enhance the security of the program. The C # compiler has a strict type security check function, which can almost find all the syntax issues in the program, which has made the programmer's busy. However, the program has passed the compilation check and does not mean that the error has not been existed. In the "wrong" big family, the status of the "syntax error" can only be considered a horn of the iceberg. High-level errors are often hidden and unbelieved.
Based on experience, many difficult program errors are due to the correct initialization or removal of variables, while initialization and clearing work are easily forgotten. Microsoft uses the object-oriented concept to fully consider this issue and solve it well: put the initialization of the object in the constructor, put the clear work in the destructor. When the object is created, the constructor is executed automatically. When the object is dying, the destructor is automatically executed. This doesn't have to worry about the initialization and clearing work of the forgotten object.
two. Application of constructor in C #
The name of the constructor cannot be casually, and the compiler must be alleged to be automatically executed. Its naming method is simple and reasonable: the constructor is the same name. In addition to the name, another special specifier of the constructor does not have a return value type, which is different from the function of the return value of Void. If it has a return value type, the compiler will not know what it is. The statement that you can access a class method, attribute, or any other thing, the first executed statement is a constructor containing the corresponding class. Even you don't write a constructor, there will be a default constructor to provide you.
Class testclass
{
Public TestClass (): base () {} // is provided by the CLR
}
There are several types of constructor.
1) Default constructor
Class testclass
{
Public TestClass (): base () {}
}
It has been described above, provided by the system (CLR).
2) Example constructor
The example constructor is a method member that implements initialization in the class. Such as:
Using system;
Class Point {public Double X, Y;
Public Point ()
{THIS.X = 0; this.y = 0;}
Public Point (Double X, Double Y)
{This.x = x; THIS.Y = Y;}
...
}
Class test {static void
Main
()
{Point a = new point (); point b = new point (3, 4); // Initialize the object with constructor ...
}
Declare a class Point that provides two constructor. They are overloaded. One is a Point constructor with no parameters and a point constructor with two Double parameters. If these constructors are provided in the class, the CLR will automatically provide a default constructor. But once the class provides custom constructor, such as Point () and Point (Double X, Double Y), the default constructor will not be provided, this should be noted.
3) Static constructor static constructor is a method member that implements initialization of a class. It is generally used in initialization of static data. The static constructor does not have parameters, and there is no modifier and cannot be called. When the class is loaded, the static constructor of the class is automatically called. Such as:
Using system.data;
Class Employee {Private Static DataSet DS;
Static Employee ()
{DS = new dataset (...);}
...}
Declare a class EMPLOYEE with a static constructor. Note that the static constructor can only initialize the static data members, and the non-static data members cannot be initialized. However, non-static constructors can be assigned to static data members or to initialize non-static data members.
If the class contains only static members, you can create a private constructor: private testclass () {...}, but Private means that the constructor cannot be accessed from the outside of the class. So, it cannot be called, and there is no object to be instantiated by this class.
The above is a simple application of several types of constructor, and the following will focus on the use of the construction function of the base class and derived class in the hierarchy of the class. The initialization of the derived class is completed by the base class and derived class: the member of the base class is initialized by the construction function of the base class, and the members of the derived class are initialized by the derived class constructor.
When you create a party's object, the system will call the constructor of the base class and the constructor of the derived class. The execution order of the constructor is: first execute the constructor of the base class, and then perform the constructor of the derived class. If the derived class has an object member, then the constructor of the base class is first executed, and the constructor of the member object class is executed, and finally the derived class constructor is executed.
As for what constructor of the base class, it is default to perform a non-refined constructor of the base class. If there is a reference function function to perform the base class, it must be pointed out in the member initialization table of the derived class constructor. Such as:
Class A
{Private INT X;
Public a () {x = 0;
Public A (INT I) {x = i;}
}
Class B: a
{Private int y;
PUBLIC B () {y = 0;}
PUBLIC B (INT I) {y = i;}
Public B (INT I, INT J): A (i) {y = j;}
}
B b1 = new b (); // Perform the constructor of the base class A (), and then execute the constructor of the derived class B ()
B b2 = new b (1); // Perform the constructor A () of the base class A, then perform the constructor of the derived class B (int)
B B3 = New B (0, 1); // Perform the construction function A (int) of the base class A, executing the derived class
Constructor B (int, int)
The order of the implementation function here is to be clearly analyzed. In addition, if there is no connectionless constructor PUBLIC A () {x = 0;} in the base class A;}, in the initialization table of all constructor members of the derived class must indicate the base class A, a reference function A (i) ,As follows:
Class A
{Private INT X;
Public A (INT I) {x = i;}
}
Class B: a
{Private int y;
Public b (): a (i) {y = 0;}
Public B (INT I): A (i) {y = i;}
Public B (INT I, INT J): A (i) {y = j;}};
three. Destructure function and garbage collector in C #
The destructor is a method member that implements an instance of a class. The destructor cannot have parameters, and it cannot be modified and cannot be called. Due to the opposite of the designer function, the prefix '~' is added to the presentation difference.
Although C # (more definitely said CLR) provides a new memory management mechanism - Automatic Memory Management (Automatic Memory Management), the release of resources can be automated automatically through "garbage collector", generally no need User intervention, but in some special cases, the destructor is required, such as the release of non-managed resources in C #.
The release of resources is usually automated through the "garbage collector", but in particular, there are some places that need attention:
1. The reference to the value type and reference type is actually no "garbage collector" to release the memory, because when they have a scope, they will automatically release the memory because they are stored in the stack;
2. Only the object instance pointed to by the reference type will be saved in the heap, and the stack is because it is a free storage space, so it does not have a survival ("stack" elastin ("stack". On behalf of the survival period, the memory is released, and it is noted that "garbage collector" works only on this area;
However, in some cases, when you need to release the non-hosting resources, you must be solved by writing code. It is usually used to release the unscrupulous resources using the patterned function, and the code segment written by the user yourself is placed in the destructor. It should be noted that if you do not use the non-hosting resource in a class, you must not define the patterned function, because the object performs a destructive function, then the "garbage collector" is called before the hosted resource is released. The function, and then truly release the hosted resource in the second time, so that the cost of deleting actions twice is mostly one. The following uses a piece of code to pattern how the function is used:
Public Class ResourceHolder
{
...
~ ResourceHolder ()
{
/ / Here is the user code segment for cleaning the unmanaged resource.
}
}
four. summary
The constructor and the desemerical function is a simple function in the form of a class, but their use is not as simple as it seems to, so flexible and correct use constructor and destructor can help you better understand CLR. Memory management mechanism, as well as resources in the system.
references:
[1] Microsoft, Oriental China. C # Language Reference Manual [M]. Beijing: Tsinghua University Press, 2001
[2] Simon Robinson, Ollie Cornes. C # Advanced Programming [M]. Kangbo. Beijing: Tsinghua University Press, 2002