C# static class,member,constructor
总体来说体现为一次性、唯一性、排他性。
static constructor只能被实例化一次。
在非static class中,所有实例拥有唯一的static member。
不能继承别人(except object),也不能被别人继承。
static 不能操作 非static(排他性)
The following list provides the main features of a static class:
-
Contains only static members.
-
Cannot be instantiated.
-
Is sealed.
-
Cannot contain Instance Constructors.
a static class remains in memory for the lifetime of the application domain in which your program resides.
A non-static class can contain static methods, fields, properties, or events.
Static methods can be overloaded but not overridden, because they belong to the class, and not to any instance of the class.
A static constructor is only called one time.
Static constructors have the following properties:
-
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.
浙公网安备 33010602011771号