Similarities and differences between class and struct in. NET

Source: Internet
Author: User

The structure shares almost all the same syntaxes with the class, but the structure is more restricted than the class:
Although the static fields of the structure can be initialized, the structure instance field declaration still cannot use the Initial Value Setting item.
The default constructor (constructor without parameters) or destructor cannot be declared in the structure.
The structure copy is automatically created and destroyed by the compiler, so the default constructor and destructor are not required.
In fact, the compiler assigns default values to all fields (see the default table) to implement the default constructor.
The structure cannot be inherited from a class or other structure.
Structure is a value type -- if an object is created from the structure and assigned to a variable, the variable contains all the values of the structure.
When copying a variable that contains a structure, all data will be copied, and any modifications made to the new copy will not change the data of the old copy.
Because the structure does not use references, the structure is not identified-two value-type instances with the same data cannot be distinguished.
All value types in C # are essentially inherited from ValueType, and the latter is inherited from Object.
The compiler can convert the value type to the reference type in a process called packing.

The structure has the following features:
The structure is the value type, and the class is the reference type.
When a structure is passed to a method, the structure is passed by passing values, rather than being passed as a reference.
Unlike classes, the new operator can be used for structure instantiation.
The structure can declare constructors, but they must contain parameters.

A structure cannot inherit from another structure or class, and cannot be the base of a class.
All structures are directly inherited from System. ValueType, and the latter is inherited from System. Object.
Structure can implement interfaces.
It is incorrect to initialize the instance field in the structure.

Differences between classes and structures
1. Value Type and reference type
Structure is value type: value type is allocated on the stack. All base types are structure types.
For example, int corresponds to the System. int32 structure, and string corresponds to the system. string Structure. More value types can be created by using the structure.
Class is the reference type: the allocation of address stack on the stack is more efficient than the stack.
However, the stack resources are limited and it is not suitable for processing large logical and complex objects.
Therefore, structure processing is a small object to be treated as a base type, while classes process a certain business logic.
Because the structure is a value type, you can create a new structure by assigning values between structures. Classes are reference types, and assigning values between classes is just copying references. Note:
1. Although the structure is different from the class type, their base types are all objects. in c #, all types of base types are objects.
2. Although the New operator is used for structure initialization, the structure object is still allocated to the stack instead of the stack.
If "new" (new) is not used, the field remains unassigned before all fields are initialized, and the object is unavailable.
2. Inheritance
Structure: it cannot be inherited from another structure or class.
Although the structure is not explicitly declared using sealed, the structure is implicit sealed.
Class: fully scalable. Unless the declared sealed is displayed, the class can inherit other classes and interfaces, and its own can also be inherited.
Note: although the structure cannot be inherited, the structure can inherit interfaces, and methods are the same as class inheritance interfaces.
Example: structure implementation Interface
Interface IImage
{
Void Paint ();
}
Struct Picture: IImage
{
Public void Paint ()
{
// Painting code goes here
}
Private int x, y, z; // other struct members
}

3. Internal Structure:
Structure:
No default constructor exists, but you can add constructor.
No destructor
No abstract and sealed (because it cannot be inherited)
The protected modifier cannot exist.
You do not need to use new for initialization.
It is incorrect to initialize the instance field in the structure.
Class:
Default constructor available
Destructor
Abstract and sealed can be used.
There is a protected Modifier
New must be used for initialization.

How to Select structure or Class
1. The stack space is limited. For a large number of logical objects, creating classes is better than creating structures.
2. structure indicates lightweight objects such as vertices, rectangles, and colors
For example, if an array containing 1000 vertex objects is declared, additional memory will be allocated to each referenced object.
In this case, the structure cost is low.
3. Classes are the best choice for presentation of abstract and multi-level object Layers
4. In most cases, this type is the best choice for structure when it is only some data.

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.