I. Differences between structures and Classes
1. The structure level is consistent with that of the class. It is written under the namespace. You can define fields, attributes, methods, and constructor methods, or create an object using the keyword new.
2. Fields in the structure cannot be assigned an initial value.
3. A constructor without parameters is automatically generated by the C # compiler no matter how it is. Therefore, a constructor without parameters cannot be defined for the structure.
4. In the constructor, all fields of the struct must be assigned a value.
5. assign values to the attribute in the constructor. It is not considered to assign values to the field, because the attribute is not necessarily a field to be operated.
6. The structure is a value type. When a structure variable is transferred, each field in the structure object is copied to the field of the new structure variable.
7. Automatic attribute cannot be defined because the field property generates a field which must be in the constructor, but we do not know the name of this field.
8. Declare the struct object without using the new keyword. However, the struct object field does not have an initial value. Because the constructor is not called, the field must be assigned a value in the constructor, create a struct object using the new keyword. The fields of this object have default values.
9. The stack access speed is fast, but the space is small, and the access speed of the stack is slow, but the space is large. When we want to represent a lightweight object, it is defined as a structure, to increase the speed, select based on the impact of the transfer. If you want to transfer a reference, it is defined as a class. If you want to transfer a copy, it is defined as a structure.
Ii. Demo
{ { x = { { y = Console.Write(, .X, Point( x, .x = .y = .p = Point( .x = .y = .p = Point( x, .x = .y = .p = Name { ; Main( Program p = Program() { Name= Point point1 = Point(, Point point2 = point2.p.Name = Console.WriteLine(point1.p.Name); }
Note: All the above articles are original articles. If you need to reprint them, please indicate the source!