Chapter 11. Serialization

The .NET Framework includes extensive support for serialization and deserialization, allowing clients to explicitly and implicitly serialize object instances and arbitrarily complex object graphs, and allowing types a wide degree of control over how and where they are serialized and deserialized.

This chapter introduces the support for object serialization and deserialization that is built into the .NET Framework, and shows how clients and objects can participate in the serialization and deserialization process. Unless otherwise stated, the types mentioned in this chapter all exist in the System or System.Runtime.Serialization namespaces.

What Is Serialization?

Some types are like mosquitoes: once instantiated, they live a short life near to where they were born, and then die quietly. Types such as these don’t store important nontransient data and are used by clients in the same context they were created in. These types are trivial, and generally don’t need serialization. However, many important types are not like this, storing data that needs to be persisted to some backing store (e.g. a database, file, or network), or being passed back and forth between different contexts as parameters in method calls. These types need to support serialization.

Serialization is the act of taking an in-memory object or object graph and flattening it into a stream of bytes that may be stored or transmitted. Deserialization works in reverse, taking a stream of bytes and reconstituting ...

Get C# in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.