Structure Structure

zhaozj2021-02-16  65

If you are familiar with the class, look at the structure and class:

Structure and class

Visual Basic .NET unified structure and class syntax, the result is that both entities support most of the same features. However, there is an important difference between structural and classes.

Same point

Structure and classes are the same in the following:

Both belong to the "container" type, indicating that they can contain other types as a member. Both have members, members can include constructor, methods, attributes, fields, constants, enumeration, events, and event handlers. The members of both have separate accessibility. For example, a member can declare as public, and the other can be declared as private. You can implement an interface. There are shared constructor, there is or no parameters. Both can disclose the default properties, as long as the attribute has at least one parameter. Both can declare and trigger events, and both can declare the commission.

difference

Structure and classes differ from the following aspects:

The structure is a value type, and the class is a reference type. The structure uses stack allocation, class uses heap assignment. All structural members are default to public; class variables and constant defaults to private, while other class members default to public. This behavior of class members provides compatibility 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. Structural members cannot be declared as protected, class members. Only the Shared Sub structure can handle events and can only use the addHandler statement; and any class can handle events and can use the Handles keyword or AddHandler statement. Structural variable declarations cannot specify initial values, New keywords, or arrays initial size, and class variables can be declared. The structure is implicitly inherited from the ValueType class, and cannot inherit from other types, and the class can inherit from any of ValueType. The structure is not inherited; the class can inherit. The structure is never terminated, so the public language runtime (CLR) does not call the Finalize method on any structure, and the class can be terminated by the garbage collector. When there is no remaining activity reference, the garbage collector will call Finalize on the class. The structure does not require a constructor; Structure can only have a non-shared constructor only when there is no parameter; the class is optional.

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 structural variable is permanently bound to a separate structure instance. But the class is a reference type, and the object variables can reference various types instances. This difference affects the use of structures and classes in the following aspects:

The structural variable use structure is implicitly containing member initialization. Therefore, DIM S AS STRUCT1 is equivalent to DIM S as struct1 = new struct1 (). When a structural variable is assigned to another, or a structure instance to the process parameter is transmitted, all current values ​​of all variable members are copied into the new structure. When an object variable is assigned to another, or a object variable is delivered to the process, only the reference pointer is copied. The value Nothing can be assigned to the structural variable, but the instance continues to remain associated with the variable. Although the assignment reinitializes the variable member, it is still possible to invoke its method and access its data member. In contrast, if the object variable is set to Nothing, it is disconnected from any class instance, and other members cannot be accessed by variables before given another instance. Object variables can have different class instances assigned to it at different times, and several object variables can simultaneously reference the same class instance. When accessing another variable to the same instance, the value of the class member affects these members. However, structural members are independently present in their own instances. Changing its value will not be reflected in any other structural variables, even in other instances declared in the same Structure. The equivalent test of the two structures must be carried out in members of the member. Two object variables can be compared using the Equals method. Equals indicates whether the two variables are pointing to the same example. STRUCTURE statement

The Structure statement can only appear in modules, namespaces, or file levels. This means that the structure can be declared in the source file or module, an interface or class, but cannot be declared inside the process. Another structure can also be defined in a structure, but cannot access the internal structure by an external structure. Instead, a data type variable of an internal structure must be declared.

They can be accessed from any location within the module or class of the declaration structure. By default, the structure is Friend. To specify accessibility in more detail, include Public, Friend, Protected Friend, or Private in the Structure statement.

Each data member must be declared. This means that each statement in the VariableDeclarations section must use DIM, Friend, Private, or Public. Since the structure cannot be inherited from the structure, the structural member cannot be protected or protected. But the structure itself can be protected or protected.

If the Option Strict is ON, the AS clause must also be included in each member declaration. The member of the DIM declaration defaults to public access, and the member of the AS clause declaration is default to the Object data type.

At least one non-shared variable or event must be defined in the structure. In the structure, only constants, attributes, and processes cannot be included, even if certain members are non-shared.

The scope of structural members is the entire structure.

The value of any data of the structure cannot be initialized into a part of its statement. The data must be initialized by the structural parameterized constructor, or the value is assigned to the member after the instance of the structure is created.

Many functions supported by structural support are the same as class support. For example, the structure can have attributes and methods, and the interface can be implemented, or a parameterized constructor can be implemented. However, there is a major difference between certain places such as inheritance, declaration, and use.

(Above information from MSDN)

?

转载请注明原文地址:https://www.9cbs.com/read-17130.html

New Post(0)