In my .proto I have some messages that have optional fields. There is no native protogen for Debian so I don’t have one to experiment with (too lazy to compile it myself :).
Could you tell me how to implement an optional field in a class in C#? I would like to have a function or whatever that idicate the field is set (in C++ I have something like hasfoo() ). In examples I found in the internet there is nothing like that.
It supports a number of patterns here, to help transition from other serializers. And note that there are options in the protobuf-net
protogento include such members for you automatically.Firstly, anything
nullis omitted; this includes bothnullreferences andNullable<T>for structs. So:will behave.
Another option is default values; using .NET conventions:
no values of 17 will be serialized.
For more explicit control, the
ShouldSerialize*pattern (fromXmlSerializer) and the*Specifiedpattern (fromDataContractSerializer) are observed, so you can do:and
These can be public or private (unless you are generating a standalone serialization assembly, which requires public).
If your main classes are coming from code-gen, then a
partial classis an ideal extension point, i.e.since you can add that in a separate code file.