6

I'm trying to make the transition from C to C# and I have a few questions about saving objects to files and serialization.

In C if you want to save a data structure, I've been taught that saving it in a text format as a string is often unnecessary and a binary file that exist as a memory snapshot is often better because it doesn't need encoding/decoding and matching strings to fields. In C# the approach appears to be different, it converts object fields separately to a string or some other format and then it reconstructs the object when necessary. I'm not sure how binary serialization works but I think it sill converts the data to some format and doesn't exist as a pure non-formatted memory snapshot.

Why is the "memory snapshot" method without any encoding/decoding not used in C# ? The only reason I can think is compatibility with other code and environments, and maybe it has to do with the complexity of objects vs regular structures.

2
  • What if object has complex properties, not just value types; then each "part" of the object will be stored on a different memory location; also these objects may include other objects. Just a simple guess but it would be really complex to execute such a logic to save objects. Commented Jul 2, 2015 at 13:08
  • One of the great cost-reductions and benefits to productivity is .NET's abstraction of memory, and hardware in general. If you need to persist with basic requirements, let the framework handle the heavy lifting (as it so often does) and mark your class with [Serializable] and use the classes available in System.Runtime.Serialization.Formatters.Binary. Commented Jul 2, 2015 at 13:20

2 Answers 2

2

In C# you don't have access to the memory layout of an object. You can't get the memory snapshot of an object, or create an object from the snapshot. In fact, you don't even have points (yes, you have them in unsafe code, but you rarely ever write unsafe code).

Even if you did have access to the memory used by the object, it might not survive being saved to disk and then reloaded. If you upgrade the .NET environment, you may get a better optimizer that decides to reorder the object's fields differently, or use different memory alignment.

So in short - no access to object memory, so you need to serialize field by field.

The upside is, that since .NET has reflection, serializing an object this way is not hard. In fact, it's a lot easier than serializing a C++ class that holds pointers to other C++ classes.

Sign up to request clarification or add additional context in comments.

1 Comment

So because of the .NET framework doing so much about memory management, you don't have direct access to memory and even if you did it would be dangerous. That makes a lot of sense, thanks ! :)
1

The .NET binary serialization format defers to the types how they would like to be stored and puts in some meta-data so that you know how to decode it (which type, what field name, etc).

This is not primarily for interop with .NET languages -- although it could be. It is for interop with past and future versions of the same objects, which can be written in a way to be resilient to those changes -- or at least know to reject them.

In C, if you change a struct in any way -- and you have just used write() to store the memory, you would probably also opt to write some meta-data so that you could know if you had the right version (and needed to convert).

Another benefit is that the .NET languages know what to do with references. By default, C would write() an address, which would be useless later. .NET also supports the idea of fields that shouldn't be serialized (like a password) -- another thing you'd need to hand-write in the C version.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.