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

  • Home
  • SEARCH
  • 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 8239915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:18:32+00:00 2026-06-07T20:18:32+00:00

I have a class that I serialize, then deserialize using Protobuf-net version r431 (probably

  • 0

I have a class that I serialize, then deserialize using Protobuf-net version r431 (probably a year old or so). The class contains an enum _type and a string called _band. In the constructor, _type is set to StationType.Other and _band is set to an empty string.

As you can see I create an object with data, serialize, then deserialize. The value of the enum _type (StationType.Streaming) is lost, while the _band (FM) is retained.

I feel this is an error, since the behavior is inconsistent. However, if I start the enum from the value of 1 instead of 0, everything works as expected (e.g. all values are retained).

Am I missing something here? See the code and the output below:

using System;
using System.IO;
using ProtoBuf;

namespace ProtoBufsWithEnums
{
    class Program
    {
        static void Main(string[] args)
        {
            stn1 = new Station{Type = StationType.Streaming, Band = "FM"};

            var ms1 = new MemoryStream();
            Serializer.Serialize(ms1, stn1); // serialize
            byte[] bytes = ms1.ToArray();

            var ms2 = new MemoryStream(bytes);
            Station stn2 = Serializer.Deserialize<Station>(ms2); // deserialize

            Console.WriteLine("Type - Original {0}, New {1}", stn1.Type, stn2.Type);
            Console.WriteLine("Band - Original {0}, New {1}", stn1.Band, stn2.Band);
        }
    }

    [ProtoContract]
    public enum StationType
    {
        [ProtoEnum(Name = "Streaming", Value = 0)]
        Streaming = 0,
        [ProtoEnum(Name = "Terrestrial", Value = 1)]
        Terrestrial = 1,
        [ProtoEnum(Name = "Other", Value = 2)]
        Other = 2,
        [ProtoEnum(Name = "Group", Value = 3)]
        Group = 3
    }

    [ProtoContract]
    public class Station 
    {
        [ProtoMember(9)]
        private StationType _type;
        [ProtoMember(10)]
        private string _band;

        public Station()
        {
            _type = StationType.Other;
            _band = "";
        }

        public StationType Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public string Band
        {
            get { return _band; }
            set { _band = value; }
        }
    }
}

Output:

Type - Original Streaming, New Terrestrial
Type - Original FM, New FM

EDIT: Found issue 251 on the protobuf-net site, not sure if I am looking at the same issue or not. Not quite sure whether it has been fixed or not.

  • 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-06-07T20:18:34+00:00Added an answer on June 7, 2026 at 8:18 pm

    Here’s the glitch:

    public Station()
    {
        _type = StationType.Other;
        _band = "";
    }
    

    protobuf-net makes some implicit assumptions about zero defaults. This works nicely in a lot of cases, but not all. The easiest way to help it here is to tell it that you are using a non-zero default:

    [ProtoMember(9), DefaultValue(StationType.Other)]
    private StationType _type;
    

    Another option would be to tell it always to serialize it:

    [ProtoMember(9, IsRequired = true)]
    private StationType _type;
    

    If you have lots of this, another option is to disable the “zero-default” behaviour, which can be done on a custom model:

    var model = TypeModel.Create();
    model.UseImplicitZeroDefaults = false;
    

    then use model.Serialize(...) etc – but note that you should store and re-use a model like this (don’t recreate it every time you need to serialize something), since that contains all the cached reflection/meta-programming output.

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

Sidebar

Related Questions

I have a class in .NET that implements IXmlSerializable. I want to serialize its
Is anyone aware of any issues when using ProtoBuf-Net to serialize/deserialize between compact framework
I have a class that I need to be able to serialize to a
I have a simple Java class that I need to serialize to be stored
I have a class that serializes a set of objects (using XML serialization) that
To Serialize a class with protobuf-net, you neet to provide the class and property
I have an ASP.NET 4 web app that is using the Entity Framework to
I have a project that contains several submodules, that communicate using some objects that
I have the following xml I'd like to deserialize into a class <?xml version=1.0
I'd like to use protobuf-net to serialize a derived class as its base class.

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.