[C# study notes] Static members and instance members

Foreword: Classes are abstractions of objects, objects are instances of classes, objects have their own properties and methods, everything is an object, and inevitably, classes are also objects. It also has its own methods and properties. Members of a class are called static members, including static data and static methods, and members of the object instantiated by the class are called instance members. 这里写图片描述

static member: Also called 类成员, refers to a variable or method modified with a static keyword before the member type or return value type, including static data and static methods. , static member stored in the thread stack , when the class is loaded into memory; Instance member: Also known as non-static member, object member, is a variable or method that is not modified with static, including instance data and instance methods, reference exists in the thread stack, The object exists in the managed heap . The difference: the surface is whether it has been statically modified, and secondly in the memory allocation, when the class is loaded, the static member is only allocated once, and there is only one storage space. For instance members, a storage space is reassigned each time an instance is generated.

The static member of the feature: 1) Static members (including static data and static methods) must be called by the class name, and cannot be called with objects (static data can be called by instance methods). 2) Static data belongs to the class level. When the class is loaded, a static data is allocated only one storage space in the memory. No matter how many instances are new, it only has that space. 3) Static methods can only call static data, not objects. Example members of the characteristics: 1) Instance members (including instance data and instance methods) must be called by object, not by class name. 2) The instance data of the class belongs to the instance level of the class. Each new object is created, and a new storage space is created in the memory for the instance member. 3) Instance methods can call instance data and static data. Attachment: The methods such as WriteLine that we often use in C# are static members, which are used directly by calling the class name Console. Let's use code to illustrate how the method calls the data: 1. class Demo 2. 3. { 4. int a; // instance data 5. static int b; // static data 6. void A () / / instance method 7. 8. { 9. a = 1; // correct: instance method can call instance data 10. b = 1; // correct: instance methods can call static data 11. } 12. 13. static void B() // static method 14. 15. { 16. a = 1; // Error: Static method cannot call instance data 17. b = 1; // correct: static methods can call static data 18. } 19. 20. static void Main() // static method twenty one. { 22. Demo d = new Demo(); // Instantiate the object 23. d.a = 1; // correct: instance can call instance data 24. d.b = 1; // Error: instance cannot call static data 25. Demo.a = 1; // Error: Class name cannot call instance data 26. Demo.b = 1; // Correct: class name can call static data 27. d.A(); // Correct: the instance can call the instance method 28. d.B(); // Error: Instance cannot call static method 29. Demo.A(); // Error: Class name cannot call instance method 30. Demo.B(); // Correct: class name can call static method 31. 32. } In addition, let's use code to illustrate that static data uses only one memory space: Public class Demo 2. 3. { 4. static int count=0; //Define static data count 5. int number=0; //Define instance data number 6. public Demo() 7. { 8. number =number+ 1; 9. count = count + 1; 10. } 11. 12. public void show() 13. { 14. Console.WriteLine("number={0} : count={1}", number, count); 15. } 16. } 17. 18. class Test 19. { 20. public static void Main() twenty one. { 22. Demo a = new Demo(); 23. a.show(); 24. Console.WriteLine("*****”); 25. Demo b = new Demo(); 26. b.show(); 27. Console.WriteLine(“*****”); 28. Demo c = new Demo(); 29. c.show(); 30. } 31. }

output result is: Number=1 : count=1**********     number=1 : count=2     **********number=1 : count=3 It can be seen that the value of static data changes with the number of objects instantiated. Each additional instantiated object will make the static data value +1, so all objects are using the same static data, that is, proof Static data shares a memory space, and the instance data of each object is the same, indicating that each instantiation will open up a new memory space.

The article part of the information comes from the network, thank you for the selfless dedication of the author.