C # BASICS (7)-differences between static and non-static classes and static members,
Static class
An important difference between static and non-static classes is that static classes cannot be instantiated.That is to say, you cannot use the new keyword to create a variable of the static class type. When declaring a class, using the static keyword has two meanings: First, it prevents programmers from writing code to instantiate the static class; second, it prevents any instance fields or methods from being declared within the class.
Static classes are introduced only from C #2.0. C #1.0 does not support static class declarations.. The programmer must declare a private constructor. The private constructor prohibits developers from instantiating class instances outside the class scope. The effect of using a private constructor is very similar to that of using a static class. The difference between the two lies in that the private constructor can still instantiate the class from inside the class, while the static class cannot be instantiated from anywhere, including from inside the class. Another difference between a static class and a private constructor is that an instance member is allowed in a class that uses a private constructor, C #2.0 and later compilers do not allow static classes to have any instance members.The advantage of using static classes is that the compiler can perform checks to ensure that instance members are not accidentally added, and the compiler will ensure that such instances are not created.Another feature of a static class is that the C # compiler automatically marks it as sealed. This keyword specifies a class as non-extensible; in other words, it cannot derive other classes from it.
Main features of static classes:
1: contains only static members.
2: it cannot be instantiated.
3: it is sealed.
4: The instance constructor cannot be included.
Static Member
1: non-static classes can contain static methods, fields, attributes, or events;
2: no matter how many instances are created for a class, its static members have only one copy;
3: static methods and attributes cannot access non-static fields and events of their contained types, and cannot access instance variables of any objects;
4: static methods can only be overloaded, but cannot be overwritten, because static methods do not belong to instance members of the class;
5: although the field cannot be declared as static constThe behavior of the const field is static in nature.. Such fields belong to the class,
Does not belong to the class instance. Therefore, you can access the const field using ClassName. MemberName notation like static fields. 6: C # does not support static local variables (static variables are defined within the method ).
Static Constructor
1: static classes can have static constructor, and static constructor cannot inherit;
2: static constructors can be used for static classes or non-static classes;
3: The static constructor has no access modifier or parameter and only one static flag;
4: The static constructor cannot be called directly. When a class instance is created or any static member is referenced, the static constructor is automatically executed and
Run only once.
Note:
1: static classes are always located in the memory;
2: non-static classes are independent in the memory after being instantiated. Their variables will not be repeated and will be destroyed in time after use, so there will be no Unknown
.In C #, static members are sensitive and should not be used unless otherwise confirmed.;
3: We recommend that you use more general classes (non-static classes ).
Usage selection:
When the defined class does not need to be instantiated, we use static classes. If you need to instantiate an object and inherit from other features, you should use non-static classes, and set the variables and methods used in a unified manner to static, so that all instance objects can be accessed.
C language ^ how to use
A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010
B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011
^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.
//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].
C language ^ how to use
A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010
B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011
^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.
//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].