Difference between struct and class in C #

Source: Internet
Author: User
According to the information provided by C # programming reference, struct is actually used to implement lightweight classes, which means it can still have attributes, methods (functions), labels, etc, the only difference is that struct cannot be programmed ProgramThe member declares the constructor. Even the default constructor (without parameters) cannot have the Destructor processing part. This is because the struct constructor is automatically generated by the compiler. The purpose of struct is to describe lightweight objects, such as line and point, with high efficiency. I don't think I can pass the interview if I have a wrong answer. I personally think that this struct really does not need to exist in the C # language. Why? Because the class can completely replace the behavior of struct, the only problem that affects efficiency is that struct is passed by passing values, while class is passed by referencing. So some people will say that if there are a large number of lightweight objects in the system, you can use struct, which is much more efficient, I think this design will come up after the brain is trampled on. You think, if a system can have lightweight objects that affect efficiency, so we should consider re-designing. What's more, the compiler still gives constructors to struct, so I personally think it has no advantage even when compiling.

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.