Structs are declared in the same way as classes, and can hold fields, methods, and constructors:
accessModifier struct UniqueName
{
Variables
Constructors
Methods
}
Like classes, any variables and methods belong exclusively to the struct and are accessed by its unique name.
However, structs have a few limitations:
- Variables cannot be initialized with values inside the struct declaration unless they're marked with the static or const modifier—you can read more about this in Chapter 10, Revisiting Types, Methods, and Classes.
- Constructors without parameters aren't permitted.
- Structs come with a default constructor that will automatically set all variables to their default values according to their type.
Every character requires a good weapon, and these weapons are the perfect fit for a struct object over a class. We'll discuss why that is in the Understanding reference and value types section of this chapter. However, first...