3.35-dev) targets .NET 4.5+, .NET Standard 1.1/2.0, and .NET 5+. The Google.Protobuf NuGet package is the easiest way to get started.
Installation
Add the NuGet package
In your project directory, run:Or add it via the NuGet Package Manager in Visual Studio by searching for
Google.Protobuf.Install the protoc tools package
The
Google.Protobuf.Tools package includes a precompiled protoc.exe and the well-known .proto files:Generate C# code from your .proto files
Invoke This generates
protoc with --csharp_out:Addressbook.cs (or multiple files if using option csharp_namespace).Generating code
The--csharp_out flag tells protoc to emit C# code. The csharp_namespace file option controls the namespace for generated types:
Working with messages
Creating and mutating messages
C# generated message classes are mutable objects. Set properties directly:Serializing and parsing
JSON support
The C# runtime includes full support for the canonical protobuf JSON format:The JSON format uses proto field names by default. Pass
JsonFormatter.Settings options to use camelCase names or to include fields with default values.Supported platforms
| Target | Framework |
|---|---|
| Desktop / server | .NET 4.5+, .NET 5+ |
| Cross-platform | .NET Standard 1.1, .NET Standard 2.0 |
| Visual Studio | 2012 and later |
Key API reference
IMessage
All generated classes implement
Google.Protobuf.IMessage<T>. Provides WriteTo(CodedOutputStream), MergeFrom(CodedInputStream), CalculateSize(), and Clone().MessageParser
Each generated class exposes a static
Parser property of type MessageParser<T>. Use Person.Parser.ParseFrom(bytes) or Person.Parser.ParseFrom(stream).JSON
JsonFormatter.Default.Format(message) for serialization. JsonParser.Default.Parse<T>(json) for parsing. Part of the Google.Protobuf package — no extra dependency needed.Well-known types
Google.Protobuf.WellKnownTypes namespace contains C# representations of Timestamp, Duration, Struct, Any, and other well-known types with special serialization behavior.