In-depth Summary of differences between c # classes and structures
Source: Internet
Author: User
Differences between classes and structures: 1. One is the reference type and the other is the value type.
Class is a reference type that inherits from the System. Object Class.
Struct is a value type that inherits from the System. ValueType class and therefore does not have polymorphism
Because the structure is a value type, you can create a new structure by assigning values between structures. When copying a variable that contains a structure, all the data is copied, changes made to the new copy do not change the data of the old copy, but the class is the reference type, and the assignment between classes is only a copy reference.
2. Differences in inheritance
Class supports inheritance and can inherit from classes and interfaces. It is completely scalable unless sealed is declared. Otherwise, the class can inherit from interfaces of other classes, and its own can also be inherited.
Struct has no inheritance. It cannot inherit from another structure or class, nor can it be inherited. Although the structure is not explicitly declared using sealed, the structural engineer implicitly sealed. Struct supports interface inheritance.
3. Internal Structure:
Structure Class There is no default (No parameter) constructor, but you can add constructor. The structure can declare constructor, but they must contain parameters.
Default constructor available
No destructor
Destructor
No abstract and sealed (because it cannot be inherited)
Abstract and sealed can be used.
You cannot have a protected modifier. You cannot declare protected, virtual, sealed, and override members.
You can declare protected, virtual, sealed, and override members.
You do not need to use new for initialization.
New Initialization is required. struct initializes the statement. All member variables are 0 or null by default.
It is incorrect to initialize the instance field in the structure.
The Struct variable is automatically released after it is used.
Class instances have garbage collection mechanism to ensure memory recovery
The object created by Struct is easy to copy and can be directly connected by equal signs.
Class Object replication is divided into shallow replication and deep replication, which must be completed through special methods.
We can simply understand that the class is an movable machine with behaviors, multiple machines with inheritance, and struct is a zero fraction, combining 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, while struct is a value type, and memory is allocated to the thread stack, this difference leads to all the differences above.
4. How to Select classes and structures
(1) consider the structure when implementing a structure that is mainly brave enough to store data.
(2) The Struct variable occupies the space of the stack. Therefore, it is suitable for scenarios with relatively small data volumes. The stack space is limited. For a large number of logical objects, creating classes is better than creating structures.
(3) structure arrays have higher efficiency
(4) structure indicates lightweight objects such as vertices, rectangles, and colors. For example, if an array of objects containing 1000 vertices is declared, additional memory will be allocated to each referenced object, in this case, the structure cost is low.
(5) class is the best choice for presentation of abstract and multi-level object Layers
(6) In most cases, this type is only the best choice for architects when some data is used.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service