In depth summary of the c class and structure of the differences

  • 2020-05-12 03:11:53
  • OfStack

Differences between classes and structures:
1.1 are reference types and 1 is value type
Class is a reference type inherited from the System.Object class
Struct is a value type and inherits from the System.ValueType class, so it is not polymorphic
Since a structure is a value type, assignment between the structures can create a new structure. When copying variables containing the structure, all data will be copied. Changes made to the new copy will not change the data of the old copy.
2. Inheritance differences
Class supports inheritance, can be inherited from classes and interfaces, and is fully extensible unless sealed is declared. Otherwise the class can inherit from the interfaces of other classes and be inherited itself.
Struct is not inheritable, cannot be inherited from another structure or class, and cannot be inherited itself. Although the structure is not explicitly declared with sealed, the structure is implicitly sealed. Struct supports interface inheritance.
3. Internal structure:
structure
class
There are no default (no-argument) constructors, but constructors can be added, and structures can declare constructors, but they must take arguments.
There are default constructors
There is no destructor
You have a destructor
No abstract and sealed
You can use abstract and sealed
You cannot have the protected modifier, and you cannot declare protected, virtual, sealed, and override members
You can declare protected, virtual, sealed, override members
You can do this without using new initialization
You must initialize with new, which was initialized at the time of the declaration, and all member variables default to 0 or null
It is an error to initialize an instance field in a structure
When the Struct variable is used up, the memory allocation is automatically unallocated
The Class instance has a garbage collection mechanism to ensure the recycling of memory
The object created by Struct is simple to copy and can be connected directly with an equal sign
The Class object replication is divided into shallow and deep replication, which must be done in a special way
We can simply understood as class is one can move the machine, has the behavior, have more than one, have inherited, and struct is a boxes, combination of different parts of the structure, it is the most essential difference is that a class class and struct reference type, memory allocation in the managed heap, and struct is a value type, memory allocation on the thread stack, this difference led to all of the above.
4. How do you select classes and structures
(1) when implementing a structure that is mainly brave to store data, structure can be considered
(2) Struct variable occupies the space of the stack, so it is suitable for situations with relatively small amount of data and limited space of the stack. For a large number of logical objects, it is better to create a class than to create a structure.
(3) structured array has higher efficiency
(4) structures represent lightweight objects such as points, rectangles, and colors. For example, if an array of objects containing 1000 points is declared, additional memory will be allocated for each object to reference. In this case, the structure costs less.
(5) classes are the best choice when it comes to representing abstractions and multilevel object hierarchies
(6) in most cases, this type is only 1 data, the best choice for the structure division.

Related articles: