the important difference between a static class and a non-static class is that a static class cannot be instantiated , that is, a variable of a static class type cannot be created with the New keyword. Using the static keyword when declaring a class has two meanings: first, it prevents programmers from writing code to instantiate the static class, and secondly, it prevents any instance fields or methods from being declared inside the class.
static classes are introduced from C # 2.0, and C # 1.0 does not support static class declarations . The programmer must declare a private constructor. A private constructor prohibits a developer from instantiating an instance of a class outside the scope of the class. The effect of using a private constructor is very similar to the effect of using a static class. The difference is that the private constructor mode can still instantiate the class from within the class, while static classes prohibit instantiating the class from anywhere, including from within the class itself. Another difference between static classes and the use of private constructors is that in classes that use private constructors, instance members are allowed, whereas C # 2.0 and later compilers do not allow any instance members of static classes. The advantage of using static classes is that the compiler is able to perform checks to ensure that instance members are not accidentally added, and the compiler will guarantee that instances of this class will not be created. another characteristic of a static class is that the C # compiler automatically marks it as sealed. This keyword specifies that the class is not extensible, in other words, you cannot derive other classes from it.
Key features of static classes:
1: Contains only static members.
2: Cannot instantiate.
3: Is sealed.
4: Cannot contain instance constructors.
1: A non-static class can contain static methods, fields, properties, or events;
2: No matter how many instances are created on a class, its static members have only one copy;
3: Static methods and properties cannot access non-static fields and events in their containing types, and cannot access instance variables of any object;
4: Static methods can only be overloaded, not overridden, because static methods do not belong to instance members of the class;
5: Although a field cannot be declared as static const, the behavior of a const field is inherently static . Such fields belong to the class,
Instances that are not part of a class. Therefore, you can use classname.membername notation to access a const field as you would with a static field; 6:c# does not support static local variables (static variables are defined inside a method).
1: Static classes can have static constructors, static constructors are not inheritable;
2: Static constructors can be used for static classes, or for non-static classes;
3: Static constructors No access modifiers, no parameters, only one static flag;
4: The static constructor cannot be called directly, and the static constructor is executed automatically before the class instance is created or any static members are referenced, and
Executes only once.
1: Static class in memory is always a position;
2: Non-static class after instantiation is in memory is independent, its variables will not be duplicated, will be destroyed in time after use, so there is no unknown
The error. static members in C # are relatively sensitive and should not be used without being very sure;
3: It is recommended to use more general classes (non-static classes).
When the defined class does not need to be instantiated, we use static classes, and if you need to instantiate an object, you should use a non-static class when you need to inherit attributes such as, and the uniformly used variables and methods are set to static, then all instance objects can be accessed.
C # Static class differs from non-static class, static member