Contents Class and structure instance comparison Differences between classes and structures How to Select structure or Class I. Comparison of classes and structures: Structure example: Public struct Person { String Name; Int height; Int weight Public bool overWeight () { // Implement something } } Class Example: Public class TestTime { Int hours; Int minutes; Int seconds; Public void passtime () { // Implementation of behavior } } Call process: Public class Test { Public static ovid Main { Person Myperson = new Person // declaration Structure TestTime Mytime = New TestTime // Declaration class } } From the above example, we can see that the declaration of the class and the declaration of the structure are very similar, but the difference between struct and class is after the qualifier, and when used, similar methods are used to define new structures and new classes. What are the specific differences between classes and structures? Ii. Differences between classes and structures 1. Value Type and reference type Structure is value type: value type is allocated on the stack. All base types are structure types. For example, int corresponds to System. int32 structure. string corresponds to system. string Structure. You can create more value types by using the structure. Class is a reference type: the reference type is allocated on the stack. The execution efficiency of stacks is higher than the execution efficiency of stacks. However, the stack resources are limited and it is not suitable for processing large logical and complex objects. Therefore, structure processing is a small object to be treated as a base type, while classes process a certain business logic. Because the structure is a value type, a value can be assigned between structures to create a new structure, while a class is a reference type. A value assignment between classes is just a copy reference. Note: 1. Although the structure is different from the class type, their base types are all objects. in c #, all types of base types are objects. 2. although the New operator is used for structure initialization, the structure object is still allocated on the stack instead of the stack. If the new operator is not used, before all fields are initialized, the field remains unassigned and the object is unavailable. 2. Inheritance Structure: it cannot be inherited from another structure or class. Although the structure is not explicitly declared using sealed, the structure is implicit sealed. Class: fully scalable. Unless the declared sealed is displayed, the class can inherit other classes and interfaces, and its own can also be inherited. Note: although the structure cannot be inherited, the structure can inherit interfaces, and methods are the same as class inheritance interfaces. Example: structure implementation Interface Interface IImage { Void Paint (); } Struct Picture: IImage { Public void Paint () { // Painting code goes here } Private int x, y, z; // other struct members } 3. Internal Structure: Structure: No default constructor exists, but you can add constructor. No destructor No abstract and sealed (because it cannot be inherited) The protected modifier cannot exist. You do not need to use new for initialization. It is incorrect to initialize the instance field in the structure. Class: Default constructor available Destructor Abstract and sealed can be used. There is a protected Modifier New must be used for initialization. 3. How to Select a structure or a class After discussing the similarities and differences between structures and classes, we will discuss how to choose a structure or a class: 1. The stack space is limited. For a large number of logical objects, creating classes is better than creating structures. 2. structure indicates lightweight objects such as dots, rectangles, and colors. For example, if an array containing 1000 vertex objects is declared, additional memory will be allocated to each referenced object. In this case, the structure cost is low. 3. Classes are the best choice for presentation of abstract and multi-level object Layers 4. In most cases, this type is the best choice for structure when it is only some data. |