First, Static class
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.
key features of static classes :
1: Contains only static members.
2: Cannot instantiate.
3: The nature of the static class is an abstract sealed class, so it cannot be inherited, nor can it be instantiated.
4: Cannot contain instance constructors.
5: If all the members under a class need to be shared, then the class can be defined as a static class.
Static classes differ from private constructors
1. Private constructors can still instantiate classes from within the class, while static classes prohibit the instantiation of classes from anywhere, including from within the class itself.
2. In a class that uses a private constructor, an instance member is allowed, and the compiler does not allow any instance members of the static class.
3. 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.
4. The C # compiler will automatically mark it as sealed. This keyword specifies that the class is not extensible, in other words, you cannot derive other classes from it.
Second, static variables
1, through the static keyword decoration, is a class, the instance member belongs to the object, when this class first loaded, all the static members under this class will be loaded.
2, static members are created only once, so static members only one copy, instance members have how many objects, there are how many copies.
3, when the class loads, all static members are created in the " Static storage ", once created until the program exits, it will be recycled.
4, when the variables need to be shared, the method needs to be called repeatedly, you can define these members as static members.
5. In a static method, the instance member cannot be called directly because the object may not exist when the static method is called.
6. The This/base keyword cannot be used in a static method because it is possible that the object does not already exist.
7, you can create objects of this class, the members of the development object in a static method operation.
8, in the instance method, can call static member, because this time static member certainly exists.
9. A non-static class can contain static methods, fields, properties, or events;
10. No matter how many instances are created for a class, its static members have only one copy;
11. Static methods and properties cannot access non-static fields and events in their containing types, and cannot access instance variables of any object;
12, static methods can only be overloaded, and cannot be overridden, because static methods do not belong to the instance members of the class;
13. Although a field cannot be declared as static const, the behavior of a const field is inherently static. Such a field belongs to a class and does not belong to an instance of the class.
Third, static method
Static methods are methods that do not belong to a particular object.
Static methods can access static member variables,
Static methods do not have direct access to instance variables and can be passed as arguments to static methods in case of instance function invocation.
A static method cannot invoke an instance method directly, it can be called indirectly, first to create an instance of a class, and then to invoke a static method through this particular object.
Iv. Static Constructors
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: Static constructors cannot be called directly, and static constructors are executed automatically and only once when a class instance is created or any static member is referenced.
For example
C # codeCopy
ClassProgram{Public Static IntI=0;PublicProgram (){i = 1; Console.Write ("The instance constructor method is called ");} Static Program () {i = 2; Console.Write (" Static constructor is executed ");} static void Main (string[] args) {console.write (PROGRAM.I); The result is 2, first, the class is loaded, all the static members are created in the static store, i=0, and then the members of the class are called, when the static constructor is called, i=2 program p = new Program (); Console.Write (PROGRAM.I); // result is 1, after strength, the instance constructor is called, I=1, because the static constructor executes only once, so it is no longer executed. } }< /c23>
Use of C # static statics