1。长久以来的概念一直是构造函数用来生成一个对象的实例,因为生成对象时需要使用构造函数。如
Class1 obj = new Class1(). 因为使用了new 关键字,所以往往认为是构造函数创建了对象。
其实不是,构造函数只是一个函数,是用来初始化对象用的,而不是用来创建对象用的,即对象内存的分配等不是由构造函数完成的,只是在创建对象的时候,总是会调用构造函数来完成一些初始化的工作。
2。Broadly speaking, a constructor is a method in the class which gets executed when its object is created.When the object of this class is instantiated, this constructor will be executed.
构造函数是一个在对象生成时被调用的函数
3。An instance constructor is a member that implements the actions required to initialize an instance of a class
Instance constructors are invoked by object-creation-expressions and through constructor-initializers.
4。Understanding the way constructors work in C# required one of the biggest mental leaps for me in coming from C++. Almost every detail of how constructors work is different between the two languages. The most glaring difference is how the type of the object varies (or doesn't vary) during construction. In C++, the vptr of the object mutates as it is constructed from the base class to the derived class, so calling virtual functions in a constructor body won't give you implementations from more-derived types. In contrast, the C# and CLR set the vptr once to the most derived type, which means virtual types are dispatched to the most derived type even when called from a base type's constructor. If those virtual types rely on the derived type's constructor body running, then watch out!
All instance constructors (except those for class object) implicitly include an invocation of another instance constructor immediately before the constructor-body
5。Variable initializers are transformed into assignment statements, and these assignment statements are executed before the invocation of the base class instance constructor. This ordering ensures that all instance fields are initialized by their variable initializers before any statements that have access to that instance are executed.
变量初始化在所有构造函数执行之前进行,这点同java不一样
6。A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced
静态构造函数用来实现那些仅执行一词的动作或者实例化静态数据。实例构造函数来初始化一个对象,静态构造函数来初始化一个类
-
A static constructor does not take access modifiers or have parameters.
-
A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
-
A static constructor cannot be called directly.
-
The user has no control on when the static constructor is executed in the program.
-
A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
-
Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
-
If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.
As with instance initializers, the static initializers are called before any static constructors are called. And, yes, your static initializers execute before the base class's static constructor.
和实例初始化器一样,静态初始化器在任何静态构造函数被调用之前被调用。同时,静态初始化器在基类的静态构造函数之前被执行。当你的类型首次被加载到应用程序空间时,
The CLR calls your static constructor automatically when your type is first loaded into an application space. You can define only one static constructor, and it must not take any arguments. Because static constructors are called by the CLR, you must be careful about exceptions generated in them. If you let an exception escape a static constructor, the CLR will terminate your program. Exceptions are the most common reason to use the static constructor instead of static initializers. If you use static initializers, you cannot catch the exceptions yourself. With a static constructor, you can.
CLR 自动调用静态构造函数。你可以仅仅定义一个静态构造函数,它必须不带任何参数。因为静态构造函数是由CLR调用的,你必须当心在它们中出现的异常。如果让 异常跳出静态构造函数,CLR将会终止你的程序。使用静态构造函数而不是静态初始化器的最常见的原因就是异常处理。如果使用静态初始化器,就不可能自己捕 获异常了。有了静态构造函数,才可以
http://www.wewill.cn/n19918c53.aspx