Static and constings seem to be related to static. It is easy to confuse when you begin school. When you don't have to use it, you can use these two keywords, but you can also, huh, This idea is not my own, almost everyone doesn't think about it, it is not thinking about it, this matter does not have the best month, and it is about the nature, huh, huh. First, be sure to figure it out, static keyword modification Although members are called static members, they are not unremnible. If there is this misunderstanding, there must be since then forget. Thestatic keyword is used to declare a type member that is unrelated to the object. This statement does not easily understand, an example:
Public class class1 () {public static x = 1; public y = 2; public class1 () {
}}} Public class class2 () {class1 myclass1 = new class1 (); myclass1.x = 2; // can not! X is a static member, cannot reference myclass1.y = 3; //
Class1.x = 2; // can class1.y = 3; // No, y is not a static member, can only reference} from the above code snippet can be seen, using static modified fields, only Can be referenced by class name, and cannot be applied by an object of an instantiated object, then what is the use of this? It is obvious, and the example is the example, regardless of the number of objects of the Class1 class, these objects The value of the X field is the same, and the modification of the X field value will be embodied in all this type of object. For example to instantiate an object Myclass2 in the Class1 class in the code of the Class2 class, Myclass1 and MyClass2 objects The value of the X member is the same, if the value of class1.x is changed to 10, the value of myclass1.x and myclass2.x is 10. C # is a fully objective language, all fields contain In the object, there is no global variable like C or VB. Here, the field using Static keyword modified a global variable. In addition to the modified variable, the Static keyword is also modified, attribute, event Wait, in addition to using Static keyword modification, only the type name can only be called. This, for the method that requires frequent use, use Static keyword modification to save memory, because the method will not be initialized frequently. Const key Define a constant value and the variable must be assigned to the declaration, and this value defined by the const keyword is not allowed to modify, an example: