1. Introduction
When talking about class and struct, we first feel that the syntax is almost the same, but the treatment is quite different. History transfers the baton from process-oriented programming to object-oriented programming, and both Class and struct carry their own destiny. In my opinion, the most essential difference between the struct hero and the class is that the class is the reference type, while the struct is the value type, and their allocation in memory is different. This article will discuss a series of differences.
2. Basic Concepts
2.1. What is a class?
Class is a basic concept of object-oriented programming. It is a type of custom data structure, usually including fields, attributes, methods, constructors, indexers, operators, and so on. Because it is a basic concept, you do not need to describe it in detail here. You can query related concepts. What we emphasize is. net, all classes are ultimately inherited from system. object class, so it is a reference type, that is to say, when a new class instance, the object stores the reference address of the actual data of the instance, and the object value is stored in managed
Heap.
2.2. What is struct?
Struct (structure) is a value type used to organize a set of related information variables into a single variable entity. All structures are inherited from system. the valuetype class is a value type. That is to say, the struct instance is allocated to the stack of the thread. It stores the value instead of the pointer to the value. So when using struct, we can treat it as a basic type class such as int and char.
3. Similarities and Differences
Similarities: similar syntax.
Differences:
Class is a reference type, inherited from the system. Object Class; struct is a value type, inherited from the system. valuetype class, so there is no polymorphism. However, note that system. valuetype is a reference type.
From the perspective of job ability, class shows behavior, while struct is often used to store data.
Class supports inheritance and can inherit from the class and interface. struct does not inherit, and struct cannot inherit from the class or be the base class of the class, but struct supports interface inheritance.
Class can declare non-argument constructor and can declare destructor. struct can only declare constructor with parameters, and cannot declare destructor. Therefore, struct does not have a custom default no-argument constructor. The default no-argument constructor simply initializes all values to their 0 values.
During instantiation, the class should use the new keyword, while struct may not use the New Keyword. struct initializes the declaration, and all member variables are 0 or null by default.
A class can be an abstract class and can declare Abstract Functions. struct is an abstract class and cannot declare Abstract Functions.
The class can declare the protected member, Virtual Member, sealed member, and override member. struct cannot, but it is worth noting that struct can reload the system. the three Virtual Methods of the object, equals (), tostring (), and gethashtable ().
The object replication of the class can be divided into shortest copy and deep copy, which must be completed through special methods. The copy of the object created by struct is simple and can be directly connected by equal signs.
The class instance is recycled by the garbage collector to ensure the memory is recycled. The struct variable is automatically released after use.
When passed as a parameter, class variables are passed by address, while struct variables are passed by value.
We can simply understand that a class is an movable machine with behaviors, polymorphism, and inheritance, while a struct is a zero fraction that combines parts of different structures. In fact, the most essential difference between class and struct is that class is a reference type, memory is allocated to the managed heap, and struct is a value type, and memory is allocated to the thread stack. This difference leads to all the differences above. Therefore, only by having a deep understanding of the content related to memory allocation can we better control it. The reference type and value type will be further compared and discussed in this series. Of course, as described in the title of this article, the use of class can basically replace any occasion of struct, class comes to the fore. Although struct has performance advantages in some aspects, in object-oriented programming, it is basically the world where class is rampant.
So some people will inevitably propose that, since the class can almost completely replace struct to implement all the functions, is there any need for struct? The answer is, at least in the following situations, we should consider using struct instead of class in view of performance considerations:
◆ When implementing a structure mainly used to store data, you can consider struct.
◆ The struct variable occupies the space of the stack, so it is only applicable when the data volume is relatively small.
◆ Structure arrays have higher efficiency.
◆ Compatibility with communication between some non-hosted code is provided.
All of these are the reasons why struct has a place. Of course there may be more to say, but I don't know.
Original translated from: http://wenku.baidu.com/view/f62ac3eae009581b6bd9eb6b.html