For beginners ,. NET static and non-static has been relatively difficult to grasp, here is a summary, introduce static class and common class, static method and instance method, static constructor and instance constructor, the difference between static and non-static fields.
Static vs common
1) Static classes cannot be instantiated, but common classes can;
2) static classes can only inherit from the System. Object base class. normal classes can inherit from any other non-static and non-sealed classes, but only one class can be inherited;
3) Static classes cannot inherit interfaces. Common classes can inherit multiple interfaces;
4) Static classes can only contain static members. Common classes can contain static and non-static members;
5) Static classes cannot be used as fields, method parameters and local variables; common classes can;
Static classes can be used to encapsulate a group of methods that are not associated with any objects, such as Math and Console classes.
Static constructor vs instance Constructor
1) The static constructor belongs to the class. It is executed only once when the class is used for the first time. The instance constructor belongs to the object and will be executed every time a new object is initialized;
2) A static constructor can be defined only once and cannot contain parameters. An Instance constructor can define overload and include parameters;
3) The static constructor can only access static fields of the type. The instance constructor can access static and non-static fields of the type;
4) Static classes cannot contain access modifiers. The default value is private.
Set the initialization of the type, such as the Instance Object required for the initialization type, and assign values to the static field of the type.
Static vs instance
1) Static Methods belong to Classes and are called through classes; instance methods belong to objects and are called through objects;
2) static methods cannot be non-static members of the callback class;
Complete a function unrelated to a specific object.
Static Field vs non-static field
Static fields belong to the class and are called through the class. Non-static fields belong to the object and are called through the object.
You can use static fields to record information about the class itself.
Via 51cto