Understanding of the static class

Original link: http://www.cnblogs.com/caster/archive/2011/08/29/2158384.html

It is a static class declared as static type and class contains only static members. You can not create a static instance of the class using the new keyword. Static class is loaded by the .NET Framework common language runtime (CLR) automatically when you load a program or namespace contains the class. Static classes and class members for data and functions can be accessed without having to create an instance of the class is created. Static class members can be used to separate data and behavior independent of any object identity: No matter what the object is changed, the data and functions will not change. When there is no class dependent behavior of the object or the data identifier, it can be used static.

  The main features of a static class are as follows:  

  • They contain only static members. 
  • They can not be instantiated. 
  • They are sealed. 
  • They can not contain an instance constructor.

  So create a static class and create a class that contains only static members and a private constructor roughly the same. Private constructors prevent class is instantiated.

  An advantage of using static class comprising:

  • Write in the stack, secure high-speed stability, but also in the implementation of the time, it is a priority.
  • The compiler can perform checks to make sure not to accidentally add an instance member.
  • The compiler will ensure that does not create real benefits of this class.

  Class is sealed and therefore can not be inherited. Static class constructor can not contain, but still declared static constructor to assign the initial value or set to a static state. Static methods belong to the class of all, to use the front of the class instance.

  Static and non-static class differences:

  Storage of non-static class is a class action stateful, such as non-static class language pack, then declared, could get the kind of language, language elements and some additional things.

  Static class can be seen as the class library is a single process, there is no "state" concept, you can use a static class.

  Non-static class can contain a static method, a static class but not non-static methods.

Reproduced in: https: //www.cnblogs.com/caster/archive/2011/08/29/2158384.html

Guess you like

Origin blog.csdn.net/weixin_30872337/article/details/94796778