C # differences between classes and structures

Source: Internet
Author: User

1. Value Type and reference type

Structure is value type: value type is allocated on the stack. All base types are structure types. For example, int corresponds to system. int32 structure. String corresponds to system. string Structure. You can create more value types by using the structure;

Class is a reference type: the reference type is allocated to the stack address;

The execution efficiency of stacks is higher than the execution efficiency of stacks. However, the stack resources are limited and it is not suitable for processing large logical and complex objects. Therefore, structure processing is a small object treated as a base type, and classes process a business logic.

Because the structure is a value type, a value can be assigned between structures to create a new structure, while a class is a reference type. A value assignment between classes is just a copy reference.

Note: 1. Although the structure is different from the class type, their base types are all objects, and all types of base types in C # Are objects;
2. although the new operator is used for structure initialization, the structure object is still allocated on the stack instead of the stack. If the new operator is not used, before all fields are initialized, the field remains unassigned and the object is unavailable.

2. Inheritance

Structure: it cannot be inherited from another structure or class. Although the structure is not explicitly declared using sealed, the structure is implicit sealed;

Class: fully scalable. Unless the declared sealed is displayed, the class can inherit other classes and interfaces, and its own can also be inherited;

Note: although the structure cannot be inherited, the structure can inherit interfaces. methods are the same as class inheritance interfaces.

3. Internal Structure:

Structure:
No default constructor exists, but you can add constructor.
No destructor
No abstract and sealed (because it cannot be inherited)
The protected modifier cannot exist.
You do not need to use new for initialization.
It is incorrect to initialize the instance field in the structure.

Class:
Default constructor available
Destructor
Abstract and sealed can be used.
There is a protected Modifier
New must be used for initialization.

4. How to Select a structure or a class

1. stack space is limited. For a large number of logical objects, creating classes is better than creating structures.
2. structure indicates a lightweight object such as vertex, rectangle, and color. For example, if an array containing 1000 vertex objects is declared, additional memory will be allocated to each referenced object, in this case, the structure cost is low;
3. classes are the best choice for presentation of abstract and multi-level object layers.
4. in most cases, this type is only the best choice for the structure of some data.

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.