The difference between a class and a struct in C #

Source: Internet
Author: User

1, the struct is a real value type (value Types), and the class is a reference type (Reference Types).

2, the structure uses stack storage (stack Allocation), and the class uses heap storage (heap Allocation).

3, all struct members are public by default, and the variables and constants of the class are the default bit private, but other class members are public by default.

4, struct members cannot be declared bit protected, while class members can.

5, a struct variable declaration cannot specify an initial value, use the New keyword to initialize an array of goods, but a class variable declaration can.

6, structs cannot declare a default constructor, that is, a non-shared constructor that does not own a parameter, but the class has no such restriction.

7, both can have a shared constructor, the shared constructor of a struct cannot have parameters, but the shared constructor of a class can be either with or without parameters.

8, structs do not allow the declaration of destructors (destructor), and classes do not have this limitation.

9, an instance of the struct (Instance) declares that initialization of the contained variable is not allowed, and the class can initialize the variable while declaring an instance of the class.

10, the struct is implicitly inherited from the ValueType class, and cannot inherit any other type, and the class can continue to any class other than ValueType.

11, a struct cannot be inherited, and a class can.

12, the structure never terminates, so the CLR does not invoke the Finalize method on any structure. Class is terminated by the memory reclamation process, which invokes the Finalize method of the class when the memory reclamation process detects a class that does not have any effect.

13, structs do not require constructors, and classes require constructors.

14, structs can only use non-shared constructors in one case, that is, non-shared constructors accept parameters. The class, however, has no such restriction, and it can use non-shared constructors with or without parameters.

14, each structure has an implicit public constructor with no parameters, which initializes all members of the struct to their default values. There is no need to redefine this behavior.

At the instance and variable level, because structs are numeric types, each structure variable is bound to the struct instance forever. However, the class is a reference type, and the object variable can refer to different instances of the class, and in this regard the difference will have the following effect on the use of the struct and the class:

15, the struct variable implicitly initializes the member with the parameterless constructor of the struct, which means that the statement struct S = new struct ().

16, when you assign a struct variable to another, or pass a struct instance to a program variable, the values of all the variable members are copied into the new structure. When you assign an object variable to another, or pass an object variable to a program, you simply copy the pointer.

17, you can assign a null value to a struct variable, but the instance remains associated with the variable. Although variable members are then reinitialized by assigning values, you can still invoke the variable's method and access its data members. Conversely, if you set the object variable to NULL, you break its association with any type instance, and you cannot access any members through the variable unless you assign another instance to it.

18, you can assign instances of different classes to the same object variable at different times, and there can be several object variables referencing the same class instance at the same time, and if you change the value of a class member, other object variables that point to the same instance will also change. However, struct members are encapsulated in their own instances, and changing struct member values does not affect members of any other struct variable, or even other instances of the same struct declaration.

19, two structures must perform an equality comparison in the way members compare members. Two object variables can be compared by using the Equals method. equals determines whether two variables point to the same instance.

How do I choose a struct or a class?

1. Stack space is limited, for a large number of logical objects, creating a class is better than creating a structure
2. Structures represent lightweight objects such as points, rectangles, and colors, for example, if you declare an array that contains 1000 point objects, additional memory is allocated for each object that is referenced. In this case, the cost of the structure is lower.
3. Classes are the best choice when performing abstract and multilevel object hierarchies
4. Most of the time the type is just some data when the structure is the best choice

The difference between a class and a struct in C #

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.