Protocol Buffers library for idiomatic .NET
4,972
stars
840
commits
C#
primary language
Sep 4, 2026
updated
protobuf-net is a contract based serializer for .NET code, that happens to write data in the "protocol buffers" serialization format engineered by Google. The API, however, is very different to Google's, and follows typical .NET patterns (it is broadly comparable, in usage, to XmlSerializer, DataContractSerializer, etc). It should work for most .NET languages that write standard types and can use attributes.
Recent changes are on the releases page; older history is in the release notes.
Free, browser-based, and nothing to install — and it all runs locally: it is a static site, so your schemas and payloads never leave your machine. You can:
.proto schema — the same code generation as the
protogen global toolDocumentation is at docs.protobuf-net.dev.
From 3.3, protobuf-net can generate serializers at build time from code-first contracts, making native AOT and trimming work — and improving cold start even on ordinary JIT builds. See the AOT documentation.
The build tools (analyzers that validate contracts, and this generator) are included in the protobuf-net package by default; opt out with <ProtoBufDisableBuildTools>true</ProtoBufDisableBuildTools>.
All stable and some pre-release packages are available on NuGet.
You can use the following command in the Package Manager Console:
Install-Package protobuf-net
| Package | NuGet Stable | NuGet Pre-release | Downloads |
|---|---|---|---|
| protobuf-net |
[ProtoContract]
class Person {
[ProtoMember(1)]
public int Id {get;set;}
[ProtoMember(2)]
public string Name {get;set;}
[ProtoMember(3)]
public Address Address {get;set;}
}
[ProtoContract]
class Address {
[ProtoMember(1)]
public string Line1 {get;set;}
[ProtoMember(2)]
public string Line2 {get;set;}
}
Note that unlike XmlSerializer, the member-names are not encoded in the data - instead, you must pick an integer to identify each member. Additionally, to show intent it is necessary to show that we intend this type to be serialized (i.e. that it is a data contract).
This writes a 32 byte file to "person.bin" :
var person = new Person {
Id = 12345, Name = "Fred",
Address = new Address {
Line1 = "Flat 1",
Line2 = "The Meadows"
}
};
using (var file = File.Create("person.bin")) {
Serializer.Serialize(file, person);
}
This reads the data back from "person.bin" :
Person newPerson;
using (var file = File.OpenRead("person.bin")) {
newPerson = Serializer.Deserialize<Person>(file);
}
<= 536870911 and not in the range 19000-19999)Inheritance must be explicitly declared, in a similar way that it must for XmlSerializer and DataContractSerializer. This is done via [ProtoInclude(...)] on each type with known sub-types:
[ProtoContract]
[ProtoInclude(7, typeof(SomeDerivedType))]
class SomeBaseType {...}
[ProtoContract]
class SomeDerivedType {...}
There is no special significance in the 7 above; it is an integer key, just like every [ProtoMember(...)]. It must be unique in terms of SomeBaseType (no other [ProtoInclude(...)] or [ProtoMember(...)] in SomeBaseType can use 7), but does not need to be unique globally.
As an alternative to writing your classes and decorating them, you can generate your types from a .proto schema using the protogen tool,
available as a multi-platform "global tool", or in your browser at
protobuf-net.dev (see Online tools, above).
In v2+, everything that can be done with attributes can also be configured at runtime via RuntimeTypeModel. The Serializer.* methods are basically just shortcuts to RuntimeTypeModel.Default., so to manipulate the behaviour of Serializer., you must configure RuntimeTypeModel.Default.
I try to be responsive to Stack Overflow questions in the protobuf-net tag, issues logged on GitHub, email, etc. I don't currently offer a paid support channel. If I've helped you, feel free to buy me a coffee or see the "Sponsor" link at the top of the GitHub page.
(top 30 of 94)
C#
97.3%
Visual Basic .NET
1.2%
Protocol Buffers library for idiomatic .NET
4,972
stars
840
commits
C#
primary language
Sep 4, 2026
updated
protobuf-net is a contract based serializer for .NET code, that happens to write data in the "protocol buffers" serialization format engineered by Google. The API, however, is very different to Google's, and follows typical .NET patterns (it is broadly comparable, in usage, to XmlSerializer, DataContractSerializer, etc). It should work for most .NET languages that write standard types and can use attributes.
Recent changes are on the releases page; older history is in the release notes.
Free, browser-based, and nothing to install — and it all runs locally: it is a static site, so your schemas and payloads never leave your machine. You can:
.proto schema — the same code generation as the
protogen global toolDocumentation is at docs.protobuf-net.dev.
From 3.3, protobuf-net can generate serializers at build time from code-first contracts, making native AOT and trimming work — and improving cold start even on ordinary JIT builds. See the AOT documentation.
The build tools (analyzers that validate contracts, and this generator) are included in the protobuf-net package by default; opt out with <ProtoBufDisableBuildTools>true</ProtoBufDisableBuildTools>.
All stable and some pre-release packages are available on NuGet.
You can use the following command in the Package Manager Console:
Install-Package protobuf-net
| Package | NuGet Stable | NuGet Pre-release | Downloads |
|---|---|---|---|
| protobuf-net |
[ProtoContract]
class Person {
[ProtoMember(1)]
public int Id {get;set;}
[ProtoMember(2)]
public string Name {get;set;}
[ProtoMember(3)]
public Address Address {get;set;}
}
[ProtoContract]
class Address {
[ProtoMember(1)]
public string Line1 {get;set;}
[ProtoMember(2)]
public string Line2 {get;set;}
}
Note that unlike XmlSerializer, the member-names are not encoded in the data - instead, you must pick an integer to identify each member. Additionally, to show intent it is necessary to show that we intend this type to be serialized (i.e. that it is a data contract).
This writes a 32 byte file to "person.bin" :
var person = new Person {
Id = 12345, Name = "Fred",
Address = new Address {
Line1 = "Flat 1",
Line2 = "The Meadows"
}
};
using (var file = File.Create("person.bin")) {
Serializer.Serialize(file, person);
}
This reads the data back from "person.bin" :
Person newPerson;
using (var file = File.OpenRead("person.bin")) {
newPerson = Serializer.Deserialize<Person>(file);
}
<= 536870911 and not in the range 19000-19999)Inheritance must be explicitly declared, in a similar way that it must for XmlSerializer and DataContractSerializer. This is done via [ProtoInclude(...)] on each type with known sub-types:
[ProtoContract]
[ProtoInclude(7, typeof(SomeDerivedType))]
class SomeBaseType {...}
[ProtoContract]
class SomeDerivedType {...}
There is no special significance in the 7 above; it is an integer key, just like every [ProtoMember(...)]. It must be unique in terms of SomeBaseType (no other [ProtoInclude(...)] or [ProtoMember(...)] in SomeBaseType can use 7), but does not need to be unique globally.
As an alternative to writing your classes and decorating them, you can generate your types from a .proto schema using the protogen tool,
available as a multi-platform "global tool", or in your browser at
protobuf-net.dev (see Online tools, above).
In v2+, everything that can be done with attributes can also be configured at runtime via RuntimeTypeModel. The Serializer.* methods are basically just shortcuts to RuntimeTypeModel.Default., so to manipulate the behaviour of Serializer., you must configure RuntimeTypeModel.Default.
I try to be responsive to Stack Overflow questions in the protobuf-net tag, issues logged on GitHub, email, etc. I don't currently offer a paid support channel. If I've helped you, feel free to buy me a coffee or see the "Sponsor" link at the top of the GitHub page.
(top 30 of 94)
C#
97.3%
Visual Basic .NET
1.2%