Does protobuf-net support nullable types?

user585058 picture user585058 · Jan 21, 2011 · Viewed 9.2k times · Source

Is it possible to generate nullable members in protobuf-net?

message ProtoBuf1 {
    optional Int32? databit = 1;
    optional Nullable<bool> databool = 2;
}

Answer

Marc Gravell picture Marc Gravell · Jan 21, 2011

Yes, but it doesn't generate them by default if you are doing codegen from .proto.

If this is just C#, of course, you don't need a .proto - just:

[ProtoContract]
public class ProgoBuf1
{
    [ProtoMember(1)]
    public int? Foo {get;set;}

    [ProtoMember(2)]
    public float? Bar {get;set;}
}

If you are working from .proto, you could consider copying and editing csharp.xslt to suit your preferred layout.