The two data types of C # are extended by the Third-struct and class

Source: Internet
Author: User

Struct can all be replaced by class. Why should I use struct? The existence is reasonable. struct has many advantages in performance. Let's take a look at the main differences between them?

  • The data types are different. struct is the value type, and class is the reference type. Therefore, they have differences between all value types and reference types. Because the stack execution efficiency is higher than the heap execution efficiency, but the stack resources are limited, it is not suitable for processing large objects with complicated logic, therefore, struct is often used to process small objects that are treated as base types, while class is used to process a certain business logic.
  • In terms of inheritance, struct can neither inherit nor be inherited, but can implement interfaces, and class can be fully extended.
  • The internal structure is different. struct can only add constructor with parameters. It cannot use abstract or protected modifiers or initialize instance fields. However, struct can override system. the three Virtual Methods of the object, equals (), tostring (), gethashtable (), and class do not have these restrictions.

Comparing the differences between struct and class, the following usage principles of struct and class can be obtained:

1 struct is preferred when it is used to represent lightweight objects such as vertices and rectangles that are mainly used to store data.

2. Class is preferred when it represents large objects with large data volumes and complex logic.

3. Class is the best choice for presentation of abstract and multi-level object Layers

 

Http://blog.csdn.net/LOVESONGFOREVER/archive/2009/11/06/4775934.aspx

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.