A summary of structures and classes in C #

Source: Internet
Author: User

To sum up, there are two differences:

1, the structure is a value type, and the class is a reference type. So the former is placed in stacks, while the latter simply stores the reference address in the stack, while the specific value is stored in the heap (heap). As shown in the following illustration:

2, according to the 1th, it can be concluded that class objects are usually used to pass large data, while structural objects are used to pass small data.

3, the class can be inherited, but the structure is not supported.

4. A struct object cannot be assigned a null value like a class object.

5. Structs cannot define a destructor like a class.

6. The structure cannot be defined as abstract as a class.

7. A method cannot be overridden in a struct unless it is the following method of type object:

Equals ()

GetHashCode ()

GetType ()

ToString ()

To allow the structure to have polymorphic properties, you can make it implement an interface.

8. The events defined in the class are thread-safe and the struct is not.

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.