Structures are similar to classes as they represent data structures and methods that are used to work with them. However, unlike classes, structures are value types and do not require heap allocation. A variable of a structure type directly contains the structure data, whereas a variable of a class type contains a reference to the data, the latter known as an object.
Structures are particularly useful for small data structures that have value semantics. Complex numbers, points in a coordinate system, or key-value pairs in a dictionary are all good examples of structures. Key to these data structures is that they have few data members, that they do not require use of inheritance or referential identity, and that they can be conveniently implemented using value semantics where assignment copies the value instead of the reference.
Structures are declared as follows.
struct-declaration:
attributesopt struct-modifiersopt Struct struct-name struct-interfacesopt struct-body
End Struct struct-nameopt ;
struct-name:
identifier
A structure declaration may optionally include the following modifiers.
struct-modifiers:
struct-modifier
struct-modifiers struct-modifier
struct-modifier:
Public
Friend
The modifiers of a structure declaration have the same meaning as those of a class declaration. A compile error occurs when the same modifier appears multiple times in a structure declaration.
A structure declaration may include an interface implementation.
struct-interfaces:
: interface-type-list
Interface implementation is described in the Interfaces: Interfaces Implementation section.
A structure body consists of declarations of the structure members.
struct-body:
struct-member-declarationsopt
See also: