C # Applicable scenarios of structures and Classes

Source: Internet
Author: User

The Structure Function in C # is very powerful and can be seen as a micro class. You can include Constructors (but with some limitations), constants, fields, methods, attributes, indexers, operators, and nested types, or even rewrite Virtual Methods in System. Object.
However, there are some differences between struct and class:
Type: The structure is a value type, and the class is a reference type.
Storage location: the structure is stored on the stack, and the class is stored on the managed stack.
Pass value: the structure passes its own copy, and the class passes the referenced copy.
Constructor: The structure cannot be a custom constructor without parameters. In the constructor, assign values to all field members.
Destructor: The structure cannot be customized.
Initialization: In structure declaration, fields cannot be initialized unless declared as const or static.
Create instance: The structure does not use new when creating an instance, and the class must use new
Inheritance: The structure does not support inheritance. Only interfaces can be implemented.
Access modifier: If the structure does not support inheritance, protected and protected internal cannot be used.
Abstract and sealed modifiers are not allowed in structure Declaration (the structure is not abstract and is implicitly sealed)
In general, we seldom use the structure, and many people do not recommend the structure, but in view of these features and operating mechanisms of the structure, rational and correct application of struct can improve the performance, save resources.
Frequently Used types, and these types are dominated by data and are declared as struct when complex operations are not required.
If the data size of a type is large and it will be frequently modified, complicated operations are involved. Even if you can use struct to implement the type, you must declare it as a class because the class can be better expanded in future upgrades.
Provides compatibility with some non-hosted code communications
In. in the. NET Framework. some elements in the Drawing namespace, such as: System. drawing. point, System. guid is implemented as struct rather than class, and the reason is also being weighed in all aspects described above
The following content comes from the Internet, which helps us better understand why the structure is used.
Blog Park "Crossing the forest"
Structure is a value type, which affects performance. However, according to the structure, this impact may be positive or negative. The positive effect is that when memory is allocated to the structure, the speed is very fast because they join or save the memory in the stack. When the structure is deleted beyond the scope, the speed is also very fast. On the other hand, as long as the structure is passed as A parameter or A structure is assigned to another structure (for example, A = B, where A and B are structures), all the content of the structure will be copied, for classes, only references are copied. In this way, there will be performance loss, depending on the size of the structure, the performance loss is also different. Note that the structure is mainly used for small data structures. But when the structure is passed to the method as a parameter, it should be passed as the ref parameter to avoid performance loss-at this time, only the address of the structure in the memory is passed, in this way, the transfer speed is as fast as the transfer speed in the class. In this case, you must note that the called method can change the value of the structure.
"Jiayuan"
To design a programming language without inheritance? ­
In fact, class inheritance costs a lot-because of inheritance, each class needs to use additional data space to store the "inheritance diagram" to represent the inheritance history of the class,
Generally speaking, it is our family tree, which stores the 18th generation of our ancestor. Only in this way can we know where we came from, genealogy must be stored in extra space.
Do not think that the space for storing the "inheritance map" is very small. If our program needs to use 10000 points to store the character form data in the game,
There are N more people in one scenario. The memory overhead is not a small amount. So we can save memory space by declaring a Point as a Struct instead of a class.

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.