This paper describes the concrete use of C # static variables and instance variables, and share them for your reference. The specific analysis is as follows:
1) Differences in syntax definitions:
A class variable is also called a static variable, and the static variable is added to it before the instance variable is not added;
An instance variable is also called an object variable, that is, a variable that does not add static;
2) Differences in program operation:
An instance variable is a property of an object, and an instance object must be created where the instance variable is allocated space before the instance variable can be used. Static variables are not part of an instance object, but belong to a class, so also known as class variables, as long as the program loads the class's bytecode, without creating any instance objects, static variables will be allocated space, static variables can be used. In summary, an instance variable must be created before it can be used by the object. Static variables can be referenced directly by using the class name;
3) Differences in running performance results:
The difference between a class variable and an instance variable is that the class variable is common to all objects, where one object changes its value, the other object gets the changed result, and the instance variable is the object's private, and an object changes its value without affecting other objects;
Take a look at the following procedure:
Using system;using system.collections.generic;using system.linq;using system.text;namespace ConsoleApplication1{ Class Program { static void Main (string[] args) { statictest A, b;//defines the class's variable a = new Statictest ();//Creates an instance of the Class A B = new S Tatictest ();//Create Instance of Class B statictest.stal = statictest.stal + 10;//Assign value to static variable by class name a.a2 = a.a2 + 10;//Assign to member A2 of instance A Statictest.stal = statictest.stal + 10;//assigns a value to a static variable through the class name b.a2 = B.a2 + 20;//assigns the member of Instance B Console.WriteLine (" Statictest.stal+{0},a.a2={1} ", Statictest.stal, A.A2);//output static variable and the value of member A2 of instance A Console.WriteLine (" Statictest.stal+{0},b.a2={1} ", Statictest.stal, B.A2);//output static variable and member of instance B A2 value console.readkey (); } } Class Statictest {public static int stal = 10;//define static variable public int a2 = 10;//Define instance variable }}
The results of the operation are as follows:
I hope this article is helpful to the study of C # program design.
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
C # static variable and instance variable instance analysis
This address: http://www.paobuke.com/develop/c-develop/pbk23479.html
Related content VS2015 C # method for generating DLL files (32/64) a detailed description of C # 's parsing of XML, JSON and other formats C # operations local files and the basic methods of saving files to a database summarize C # Basic syntax: Method parameters
C # Methods for drawing ellipses C # Solving the problem of democ# in the case of a dynamic array usage example in C # asynchronous delegate invocation instance analysis
C # static variable and instance variable instance analysis