Differences between struct and classes in C #

Source: Internet
Author: User
Differences between struct and classes in C #

 

1. struct is a value type, and class is a reference type

Although the new operator is used for struct initialization, struct objects are still allocated to Stacks rather than stacks. If the new operator is not used ),

Before initializing all fields, the fields remain in the unassigned status and the object is unavailable.

2. struct cannot be inherited from another structure or class. Although the structure is not explicitly declared using sealed, the structure is implicitly sealed.

The class is completely extensible. Unless the declared sealed is displayed, the class can inherit other classes and interfaces, and its own can also be inherited.

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

3. 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.

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.