Structs
Declaring structs
A struct is something like a wrapper for different values. It is a value type and allocated on the stack (in .NET, this can differ for other backends). A struct can only contain values.
The syntactic structure of a struct:
An automatic constructor will be generated for the struct to initialize all fields.
A simple example:
Working with structs
To create an instance of a struct you have to call the constructor.
The code above creates a new instance of the struct Point
and calls the constructor to initialize the field X
with 5 and the field Y
with 8.
To index an instance of a struct by one of its fields, use the .
-operator.
Last updated