---
title: "Struct types"
description: "Learn how to define and use structs in Slice."
url: https://docs.icerpc.dev/slice/language-guide/struct-types
docs_index: https://docs.icerpc.dev/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.icerpc.dev/llms.txt).

# Struct types

Learn how to define and use structs in Slice.

## Composite type

A struct is a user-defined type that holds a list of [fields](https://docs.icerpc.dev/slice/language-guide/fields.md). For example:

```slice
enum StateAbbreviation : uint8 { AL, AK, AZ, AR, AS, WY }

struct PostalAddress {
    recipientFullName: string
    streetAddress1: string
    streetAddress2: string?
    city: string
    state: StateAbbreviation
    zip: string
}
```

## Tagged fields

The fields of a struct can be regular fields (like the fields shown earlier) or [tagged fields](https://docs.icerpc.dev/slice/language-guide/fields.md#tagged-fields).

## Compact struct

A compact struct is a struct that cannot have tagged fields—unlike a regular struct, it cannot be augmented without breaking on-the-wire compatibility.

For example:

```slice
compact struct BytePair { first: uint8, second: uint8 }
```

The encoding of a compact struct is slightly more compact than the encoding of a regular struct: `compact` saves one byte per instance.

## C# mapping

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

```slice
struct PostalAddress {
    recipientFullName: string
    streetAddress1: string
    streetAddress2: string?
    tag(1) streetAddress3: string?
    city: string
    state: StateAbbreviation
    zip: string
}
```

```csharp
internal partial record struct PostalAddress
{
    internal required string RecipientFullName { get; set; }
    internal required string StreetAddress1 { get; set; }
    internal string? StreetAddress2 { get; set; }
    internal string? StreetAddress3 { get; set; }
    internal required string City { get; set; }
    internal StateAbbreviation State { get; set; }
    internal required string Zip { get; set; }

    // Primary constructor.
    [SetsRequiredMembers]
    internal PostalAddress(
        string recipientFullName,
        string streetAddress1,
        string? streetAddress2,
        string? streetAddress3,
        string city,
        StateAbbreviation state,
        string zip)
    {
        this.RecipientFullName = recipientFullName;
        this.StreetAddress1 = streetAddress1;
        this.StreetAddress2 = streetAddress2;
        this.StreetAddress3 = streetAddress3;
        this.City = city;
        this.State = state;
        this.Zip = zip;
    }

    // Decoding constructor.
    internal PostalAddress(ref SliceDecoder decoder)
    {
        ...
    }

    // Encodes this struct.
    internal readonly void Encode(ref SliceEncoder encoder)
    {
        ...
    }
}
```

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](https://code.icerpc.dev/csharp/0.7.x/api/reference/ZeroC.Slice.Codec.SliceDecoder.html). The generated `Encode` method encodes the struct fields with a [SliceEncoder](https://code.icerpc.dev/csharp/0.7.x/api/reference/ZeroC.Slice.Codec.SliceEncoder.html).

### cs::readonly attribute

You can map a Slice struct to a readonly C# struct with the `cs::readonly` [attribute](https://docs.icerpc.dev/slice/language-guide/attributes.md). This attribute does not accept any argument. For example:

```slice
[cs::readonly]
compact struct Point { x: int32, y: int32 }
```

```csharp
internal readonly partial record struct Point
{
    internal int X { get; init; }
    internal int Y { get; init; }

    ...
}
```

You can also apply `cs::readonly` to a struct field to map this field to a get-init property.
