Difference between Class and Struct in C #

Source: Internet
Author: User

1) class is a reference type, inherited from System. Object; stuce is a value type, inherited from System. ValueType class, so there is no polymorphism. However, note that System. ValueType is a reference type.
2) from the perspective of job ability, class shows behavior, while stuct is often used to store data.
3) class supports inheritance and can inherit from the class and interface. struct does not inherit, and struct cannot inherit from the class or be the base class of the class, but struct supports interface inheritance.
4) classs can declare non-argument constructor and destructor. struct can only declare constructor with parameters, and cannot declare destructor. Therefore, struct does not have a custom default no-argument constructor. The default no-argument constructor simply initializes all values, such as its 0 value.
5) during instantiation, the class should use the new keyword, while the struct can not use the new Keyword. For struct, if there is no parameter for the instance, there will be no new; however, if a constructor with parameters is generated, new is required. If struct is not instantiated with "new", all its fields will be unallocated until all fields are initialized. Otherwise, referencing unassigned fields will cause compilation errors.

6) A class can be an abstract class and can declare abstract Functions. struct cannot declare abstract functions either.
7) class can declare protected members, virtual members, sealed members, and override members. struct cannot, but it is worth noting that struct can reload System. three Virtual Methods of the Object, (Equals (), Tostring (), GetHashTable ())
8) object replication of a class can be divided into shortest copy and deep copy, which must be completed through special methods. The copy of an object created by struct is simple and can be directly connected by equal signs.
9) the class instance is recycled by the garbage collector to ensure the memory is recycled. The struct variable is automatically released immediately after it is used.
10) When passed as a parameter, the class variable is passed by address, while the struct variable is passed by value.

We can simply understand that the class is an movable machine with behaviors and polymorphism, while the struct is a zero fraction combining different structural parts.
Of course, the use of class can basically replace struct any occasion, it can be said that in object-oriented programming, the world is rampant. Class. But some people do not know that this is a useful struct.

A) when implementing a structure for data storage, you can consider struct.
B) The struct variable occupies the stack space. Therefore, it is applicable only when the data volume is small.
C) Structure arrays have higher efficiency.
D) provides compatibility with communication between some non-hosted code.

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.