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 7790263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:31:52+00:00 2026-06-01T21:31:52+00:00

My question is whether it is possible to determine the serialized size (in bytes)

  • 0

My question is whether it is possible to determine the serialized size (in bytes) of a reference type.

Heres the situation:

I am using the BinaryFormatter class to serialize basic .NET types, ie for instance:

[Serializable]
public class Foo
{
    public string Foo1 { get; set; }
    public string Foo2 { get; set; } 
}

I am serializing each item to a byte[], then adding that segment to the end of an existing byte[] and additionally adding a carriage return at the end of each segment to delimit the objects.

In order to deserialize I use Marshal.ReadByte() as follows:

List<byte> buffer = new List<byte>();

for (int i = 0; i < MapSize; i++)
{
    byte b = Marshal.ReadByte(readPtr , i); 

    if (b != delim)  // read until encounter a carriage return 
        buffer.Add(b);
    else
        break;
}

readPtr = readPtr + buffer.Count + 1; // incrementing the pointer for the next object

return buffer.ToArray(); 

I believe that using Marshal.Copy() would be more efficient but I need to know the length of the serialized byte segment in advance. Is there a way I can reliably compute this from the type thats being serialized, or an overall more efficient method I can use?

Also, the use of a carriage return won’t be reliable, ultimately. So I am wondering if there is a more standard way to delimit the objects, either through customizing my BinaryFormatter or using some other standardized best practice? For instance is there a specific way that the BinaryFormatter delimits objects if its serializing say, a generic List<>?

  • 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-01T21:31:53+00:00Added an answer on June 1, 2026 at 9:31 pm

    There isn’t a terribly good way to determine the serialized length beforehand. The specification for the BinaryFormatter protocol is available here:
    http://msdn.microsoft.com/en-us/library/cc236844(v=prot.10).aspx

    I’ll save you the trouble of reading it for your purposes:

    1. It’s built to be an extensible format. This allows you to add fields later and still maintain some compatibility with earlier implementations. For your purposes, this means that the length of the serialized form is not fixed in time.
    2. It’s extremely fragile. The binary format actually encodes the names of the fields in it. If you ever rename a field, the length of the serialized form will change.
    3. The binary format actually encompasses a many-to-one relationship between serialized encodings and object data. The same object could potentially be encoded in a number of different ways, with a number of different byte counts for the output (I won’t get into why it’s written that way).

    If you want an easy way to do things, just create an array that contains all the objects and serialize that single array. This solves most of your problems. All the issues of delimiting the different objects are handled by the BinaryFormatter. You won’t have excessive memory copying. The final output will be more compact because the BinaryFormatter only has to specify the field names once per invocation.

    Finally, I can tell you that the extra memory copy is not the main source of inefficiency in your current implementation. You’re getting far more inefficiency from the BinaryFormatter’s use of reflection, and the fact that it encodes the field names in the serialized output.

    If efficiency is paramount, then I would suggest writing some custom code that encodes the contents of your structures in “plain old data” format. Then you’ll have control over how much gets written and how.

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

Sidebar

Related Questions

Is it possible to determine programatically whether any background applications/services are using the sattelite
Is it possible to determine via reflection whether a field is of generic type
This is a very general question: I was wondering whether it is possible to
Quick question - is it possible to detect whether a focus has come from
Being faced with the question whether it's possible to choose #include s in the
I just asked a question about whether it was possible to write a web-page-checking
My question is whether it is possible to have 2 different CSS3 transitions operating
Is it possible to determine the type of a local variable at runtime in
The question of whether P=NP is perhaps the most famous in all of Computer
My question is whether or not there is a way to specify the default

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.