Loading Protobuf to C# Converter...
Please wait a moment

How to Generate C# Classes from a .proto File

You have a Protocol Buffers schema from another team or an API, and you want to read its messages in a .NET project without setting up protoc. This page writes the classes for protobuf-net, a code-first Protobuf library: ordinary C# properties with the field numbers in attributes.

Step 1

Paste or upload your .proto file

Paste the schema into the left editor or upload a .proto file. proto3, proto2 and edition 2023 files all work. Imports are not fetched, so paste every file whose messages you need, or keep the placeholders the page writes for the missing ones.

Example: a small proto3 schema

syntax = "proto3";

package shop.v1;

// An order as stored in the billing system
message Order {
  int64 id = 1;
  string sku = 2;
  sint64 balance_delta = 3;
  repeated string tags = 4;
  map<string, int32> stock = 5;
  optional double discount = 6;
}
Step 2

Read the generated C#

Each message becomes a [ProtoContract] class and each field a [ProtoMember] property with the same field number. Nested messages and enums go into a Types class inside their parent, like protoc does. Anything the page had to decide for you is listed as a Note at the top of the output.

What the page writes for that schema

balance_delta is a sint64, so it keeps DataFormat.ZigZag; discount is marked optional, so it becomes a double? that stays null when the field isn't sent.

// C# classes for protobuf-net, generated from a .proto file.
// Add the package:  dotnet add package protobuf-net
// Read:   var msg = ProtoBuf.Serializer.Deserialize<Order>(stream);
// Write:  ProtoBuf.Serializer.Serialize(stream, msg);

#nullable disable
using System.Collections.Generic;
using System.ComponentModel;
using ProtoBuf;

namespace Shop.V1
{
    /// <summary>
    /// An order as stored in the billing system
    /// </summary>
    [ProtoContract]
    public partial class Order
    {
        [ProtoMember(1, Name = "id")]
        public long Id { get; set; }

        [ProtoMember(2, Name = "sku")]
        [DefaultValue("")]
        public string Sku { get; set; } = "";

        [ProtoMember(3, Name = "balance_delta", DataFormat = DataFormat.ZigZag)]
        public long BalanceDelta { get; set; }

        [ProtoMember(4, Name = "tags")]
        public List<string> Tags { get; } = new List<string>();

        [ProtoMember(5, Name = "stock")]
        public Dictionary<string, int> Stock { get; } = new Dictionary<string, int>();

        [ProtoMember(6, Name = "discount")]
        public double? Discount { get; set; }
    }
}
Step 3

Add protobuf-net and use the classes

Copy or download Generated.cs, add the protobuf-net package to your project and call Serializer.Deserialize and Serializer.Serialize. The classes read and write the same wire format as the code protoc generates for other languages, so data can go back and forth between them.

// dotnet add package protobuf-net
using ProtoBuf;
using Shop.V1;

byte[] bytes = File.ReadAllBytes("order.bin");  // written by any Protobuf library
var order = Serializer.Deserialize<Order>(new MemoryStream(bytes));
Console.WriteLine(order.Id);                     // 8901240544102066246, every digit

using var output = File.Create("order-copy.bin");
Serializer.Serialize(output, order);

Going the other way? C# to Protobuf writes a .proto file from your C# classes, and Proto File Validator checks a schema before you generate code from it.

Frequently Asked Questions

What do the generated classes need to compile?

Only the protobuf-net NuGet package (dotnet add package protobuf-net). The attributes [ProtoContract], [ProtoMember], [ProtoMap] and [ProtoEnum] come from it, and so do Serializer.Deserialize and Serializer.Serialize. If you would rather have the Google.Protobuf style of classes with Parser and WriteTo methods, run protoc with --csharp_out (or the Grpc.Tools package) instead; this page writes plain classes you can read and edit.

How are Protobuf field types turned into C# types?

int32 becomes int, int64 long, uint32 uint, uint64 ulong, double and float stay double and float, bool is bool, string is string and bytes is byte[]. sint32 and sint64 get DataFormat.ZigZag, the fixed and sfixed types get DataFormat.FixedSize, so the bytes on the wire match. repeated fields become List<T> (packed where proto3 packs them), map fields become Dictionary<K, V>, enums become C# enums with the original names kept in [ProtoEnum].

Will a long ID like 8901240544102066246 come through exactly?

Yes. The C# type comes from the field type in the .proto file, never from a guess, so an int64 field is a long and a uint64 field a ulong. Both hold every 64-bit value, so 8901240544102066246, -9223372036854775808 and 18446744073709551615 are read back with every digit. Number defaults in proto2 files are shown in a comment exactly as you typed them.

How are optional fields, oneofs and well-known types handled?

Fields that can be missing (proto3 optional, proto2 optional and oneof members) become nullable properties, so null means the field was not sent and 0 means it was sent as 0. A oneof is a set of nullable properties; set only one of them. google.protobuf.Timestamp and Duration become DateTime? and TimeSpan?, the wrapper types such as Int64Value become long? and similar, and a few others like Any and Empty get small classes with the same field numbers.

What about imports, services and Protobuf data files?

Imported files are not downloaded. When a field uses a type from a file you did not paste, the output gets an empty placeholder class under Imported and a note, so it still compiles; generate the other file too and swap it in. Services are listed as comments because gRPC stubs need protoc or protobuf-net.Grpc. Text format data and Base64 binary messages are not schemas, so they get a message pointing to Protobuf to JSON instead of made-up code.

Is my .proto file uploaded anywhere?

No. The schema is read and turned into C# in your browser; nothing is sent to our server and nothing is kept. A reload starts with an empty editor.