Struct types

Learn how to define and use structs in Slice.

A struct is a user-defined type that holds a list of fields. For example:

A Slice struct maps to a public C# record struct with the same name. For example:

The mapped C# record struct provides a primary constructor with parameters for all its fields, and also a decoding constructor that constructs a new instance by decoding its fields from a SliceDecoder. The generated Encode method encodes the struct fields with a SliceEncoder.

You can map a Slice struct to a readonly C# struct with the cs::readonly attribute. This attribute does not accept any argument. For example:

slice
[cs::readonly]
compact struct Point { x: int32, y: int32 }
C#
public readonly partial record struct Point
{
public readonly int X;
public readonly int Y;
...
}

You can also apply cs::readonly to a struct field to map this field to a read-only C# field.

Was this page helpful?