User-Defined Types

2 min read

2 min read

2 min read

2 min read

2 min read

2 min read

2 min read

2 min read

2 min read

Slice has five kinds of user-defined types:

  • An enumeration is a type with a fixed set of named values, such as enum Fruit { Apple, Pear, Orange }.
  • A structure groups named fields, such as the hour, minute, and second of a time of day.
  • A class groups named fields like a structure, and also supports inheritance, optional fields, null values, and graphs of instances.
  • A sequence is a variable-length list of elements of the same type.
  • A dictionary maps keys of one type to values of another type.

You can combine these types: a structure can hold a sequence, and a dictionary can map keys to structures.

Use a structure rather than a class unless you need one of the capabilities of classes: a class instance takes more time to marshal and unmarshal, and more space on the wire, than a structure with the same fields.