Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6055253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:14:41+00:00 2026-05-23T08:14:41+00:00

In my .proto I have some messages that have optional fields. There is no

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:14:41+00:00Added an answer on May 23, 2026 at 8:14 am

    It supports a number of patterns here, to help transition from other serializers. And note that there are options in the protobuf-net protogen to include such members for you automatically.

    Firstly, anything null is omitted; this includes both null references and Nullable<T> for structs. So:

    [ProtoMember(1)]
    public int? A {get;set;}
    

    will behave.

    Another option is default values; using .NET conventions:

    [ProtoMember(2), DefaultValue(17)]
    public int B {get;set;}
    

    no values of 17 will be serialized.

    For more explicit control, the ShouldSerialize* pattern (from XmlSerializer) and the *Specified pattern (from DataContractSerializer) are observed, so you can do:

    [ProtoMember(3)]
    public string C {get;set;}
    
    public bool ShouldSerializeC() { /* return true to serialize */ }
    

    and

    [ProtoMember(4)]
    public string D {get;set;}
    
    public bool DSpecified {get;set;} /* return true to serialize */
    

    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 class is an ideal extension point, i.e.

    partial class SomeType {
        /* extra stuff here */
    }
    

    since you can add that in a separate code file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple form that takes some fields and adds them to the
I my framework I have some functions that when done they can add some
I have noticed that in some apps they use what appears to be the
I have a parrallelized C++ program that does brute force optimization. For some reason
How to create a photo viewer in wp7. I have some 10 to 25
I am having some trouble creating a seamless photo grid. I have searched the
I have the mytest.cc and mytest.h output from a mytest.proto file, but I can't
I have photo gallery code that does image re-sizing and thumbnail creation. I use
I have a photo gallery I would like that every time a user does
So we have a photo like this (source: vkontakte.ru ) How to detect that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.