Absrtact: Constructors and destructors are two types of functions that seem to be simpler in a class, but there are always some unexpected running errors in the actual application process. This paper will introduce the principle of constructor function and destructor and the application in C #, and some matters needing attention in the process of using.
Keywords: constructors, destructors, garbage collector, unmanaged resources, managed resources
A The principle of constructors and destructors
As a more advanced language than C, C # provides a better mechanism to enhance the security of your programs. The C # compiler has a strict type security check function that can almost find all the grammatical problems in the program, which is really helpful to the programmer. But the program passed the compile check does not mean that the error no longer exists, in the "wrong" family, "grammatical error" status can only be regarded as the tip of the iceberg. High-level bugs are often hidden deep and are not easy to spot.
According to experience, many bugs are difficult to detect because the variables are not properly initialized or eliminated, and initialization and cleanup work can easily be forgotten. Microsoft uses object-oriented concepts to design the C # language to fully consider this problem and solve it well: put the initialization of the object in the constructor, and put the cleanup work in the destructor. When an object is created, the constructor is executed automatically. When the object dies, the destructor is automatically executed. This eliminates the need to worry about the initialization and cleanup of forgotten objects.
Two The application of constructors in C #
The name of the constructor is not random, and must be recognized by the compiler to be executed automatically. Its naming method is both simple and reasonable: let the constructor have the same name as the class. In addition to the name, another special function of the constructor is that there is no return value type, which is different from a function that returns a value type of void. If it has a return value type, then the compiler will be overwhelmed. Before you can access a class's methods, properties, or anything else, the first executed statement is a constructor that contains the corresponding class. Even if you do not write a constructor yourself, there will also be a default constructor provided to you.
class TestClass
{
public TestClass(): base() {} // 由CLR提供
}
下面列举了几种类型的构造函数
1)缺省构造函数
class TestClass
{
public TestClass(): base() {}
}
As described above, it is provided by the system (CLR).
2) Instance Constructors
An instance constructor is a method member that implements the initialization of an instance in a 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); // 用构造函数初始化对象
…
}
}
Declares a class point, which provides two constructors. They are overloaded. One is the point constructor with no arguments and a point constructor that has two double arguments. If these constructors are not provided in the class, then the CLR will automatically provide a default constructor. However, once a custom constructor is provided in a class, such as point (), or point (Double x, double y), the default constructor will not be provided, and this should be noted.