Structure and class-structure and class!

Source: Internet
Author: User
Structure and class-structure and class!

 

 

Visual Basic. Net unifies the structure and class syntax. The result is that both objects support most of the same functions. However, there are important differences between structures and classes.

Similarities

The structure and class are the same in the following aspects:

    • Both belong to the "Container" type, indicating that they can contain other types as members.
    • Both have members that can include constructor, method, attribute, field, constant, enumeration, event and Event ProcessingProgram.
    • Both members have independent accessibility. For example, a Member can be declaredPublicAnd the other can be declaredPrivate.
    • All interfaces can be implemented.
    • All have shared constructors, with or without parameters.
    • Both of them can publish the default attribute, as long as the attribute has at least one parameter.
    • Both can declare and trigger events, and both can declare the delegate.
Differences

The structure and class are different in the following aspects:

    • The structure is the value type, and the class is the reference type.
    • Structure uses stack allocation, and class uses heap allocation.
    • All structure members arePublicBy default, class variables and constants arePrivateAnd other class members arePublic. This line of the class member is compatible with the Visual Basic 6.0 default system.
    • The structure must have at least one non-shared variable or event member, and the class can be completely empty.
    • The structure member cannot be declaredProtected, Class Members can.
    • OnlyShared subThe structure process can only process events and useAddhandlerStatement. Any class process can process events and useHandlesKeyword orAddhandlerStatement.
    • The Structure Variable declaration cannot specify the initial value,NewKeyword or the initial size of the array. The class variable declaration is acceptable.
    • Structure slaveValuetypeClass implicit inheritance, cannot inherit from other types, the class can be divided fromValuetypeAny other class.
    • Structures cannot be inherited, while classes can be inherited.
    • The structure is never terminated, so the Common Language Runtime Library (CLR) never calls any structure.FinalizeMethod. The class can be terminated by the garbage collector. When no reference to the remaining activity is detected, the garbage collector callsFinalize.
    • Constructor is not required for the structure, but for the class.
    • A non-shared constructor can exist only when no parameters exist in the structure. classes can be used regardless of whether there are any parameters.

Each structure has an implicit public constructor without parameters. This constructor initializes all data members of the structure to the default value. This behavior cannot be redefined.

Instances and variables

Since the structure is a value type, each structure variable is permanently bound to a separate structure instance. The class is a reference type, and object variables can reference instances of different types. This difference affects the use of structures and classes in the following ways:

    • The Structure Variable uses the non-parameter constructor of the structure to implicitly include the member initialization. Therefore,Dim s as struct1EquivalentDim s as struct1 = new struct1 ().
    • When one structure variable is assigned to another, or a structure instance is passed to a process parameter, the current values of all variable members are copied to the new structure. When one object variable is assigned to another, or an object variable is passed to the process, only the reference pointer is copied.
    • You can set the valueNothingAssign to the structure variable, but the instance continues to be associated with the variable. Although the value assignment reinitializes a variable member, you can call its method and access its data member. In contrast, if you set the object variableNothing, Disconnects it from any class instance. Before assigning it to another instance, you cannot access other members through variables.
    • Object variables can have different class instances assigned to them at different times. Several object variables can reference the same class instance at the same time. When the access is directed to another variable of the same instance, the value of the modified class member will affect these members. However, structure members exist independently in their own instances. Changing its value is not reflected in any other structure variable, even in the sameStructureOther declared instances.
    • The equivalence test of the two structures must be performed in the member-to-member test. Two object variables can be usedEqualsMethod.EqualsIndicates whether two variables point to the same instance.

 

Differences between struct and class

 

Struct and classes are similar in many places and are a lightweight alternative to classes. Therefore, finding out their differences and similarities can better grasp their use.

Comparison:

Comparison content

Structure

Class

Constructor

Supported (it cannot be customized. It is provided by default to set zero for all data members)

Supported (customizable)

Destructor

Not Supported

Supported

Variable Initialization

Not supported (Private int num = 50Yes)

Supported

Inheritance

Not supported (SealedType)

Supported

Interface

Multiple Interfaces can be implemented

Multiple Interfaces can be implemented

Features

Supported

Supported

Attribute

Supported

Supported

Method

Supported

Supported

Type

Value Type

Reference Type

Summary:

1: Struct is implicitly derived fromObejectDerived

2: The difference between types can be seen:

Arrays of struct types are more efficient than arrays of classes (binning and unboxing are not required ). Struct set (for exampleHashtable) Efficiency is lower than the class set efficiency. The element of the set is of reference type, so the struct must be packed and disassembled. Therefore, classes are more efficient in large sets.

 

 

When should I use the structure?

It is only used to save data and uses a structure for high performance requirements.

In other cases

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.