Skip to the content.

Experimental. This is what the generator handles today. Anything else is reported as a build error, never silently mis-serialized.

The shape of a contract

[ProtoContract]
partial struct Address
{
    [ProtoMember(1)] public string City;
    [ProtoMember(2)] public int Zip;
}

Member types

C# type DataFormat protobuf type
int default int32
long default int64
long ZigZag sint64
uint default uint32
uint FixedSize fixed32
double default double
string default string (UTF-8)
a [ProtoContract] struct default nested message
repeated int default; IsPacked optional repeated int32
repeated [ProtoContract] struct default repeated message

A scalar is written only when it is not zero, and a string only when it is not null. That is protobuf’s (and protobuf-net’s) default behaviour.

Presence: {Name}Specified

protobuf-net’s convention applies. Declare a bool named after the member plus Specified, and it decides whether the member is written, in place of the zero test:

[ProtoMember(4)] public Address Address;
public bool AddressSpecified;

A nested message is a struct and can never be null, so without a Specified member it is always written.

Repeated members

On the write side, a repeated member is anything foreach can walk. The generator matches the compiler’s own pattern (a GetEnumerator() whose result has MoveNext() and Current), so the call is bound at compile time:

[ProtoMember(3, IsPacked = true)] public ReadOnlySpan<int> Scores;
[ProtoMember(9)] public ReadOnlySpan<Address> History;

A span of non-constant structs can be built on the stack in a zero app, with no allocation:

person.History = [new Address { City = "Oxford", Zip = 1 }, new Address { City = "Leeds", Zip = 2 }];

The contract’s local must then be declared scoped, so the compiler knows it does not outlive the stack buffer.

On the read side, a repeated field is a lazy view: RepeatedInt32, or RepeatedMessages<TView> for messages. It records where the field lives in the payload, validates every occurrence when the message is read, and decodes on foreach. It needs no storage, and it accepts packed and unpacked forms and occurrences scattered through the message:

case 3: ok = RepeatedInt32.TryAccumulate(ref reader, wt, body, 3, ref value.Scores); break;
case 9: ok = RepeatedMessages<AddressView>.TryAccumulate(ref reader, wt, body, 9, ref value.History); break;

The trade-off is that a view describes one payload. Merging a second payload into a view that already holds repeated data is refused (TryDeserialize returns false), rather than losing the first payload’s elements.

Views

Until the generator writes them, a view is hand-written. It is a ref struct implementing IProtoView<TSelf> with one method, TryMerge, which loops over the fields with ProtoReader. See Getting started for a complete one. The rules:

Diagnostics

id meaning
PBZ0001 a member’s type, or its DataFormat, is not supported
PBZ0002 the contract itself cannot be generated (nested or generic)
PBZ0003 a member’s type is a contract that could not be generated

PBZ0003 exists so that one unsupported member does not produce a cascade of compiler errors in generated code. The contract that cannot be generated is reported, and so is everything that reaches it.