-
Notifications
You must be signed in to change notification settings - Fork 1
Serialization
Serialization is the process of persisting the state of an object to a file or memory buffer. The persisted data contains all the necessary information you need to reconstruct (deserialize) the state of the object.
Serializable marks a class serializable.
[Serializable]
public class UserPrefs
{
public string WindowColor;
public int FontSize;
}Example using BinaryFormatter to write a Serializable object to a file stream.
UserPrefs userData = new UserPrefs { WindowColor = "Blue", FontSize = 24 };
BinaryFormatter bf = new BinaryFormatter();
using (Stream fs = new File.OpenWrite("user.dat"))
{
bf.Serialize(fs, userData);
}The CLR accounts for all related objects as well to ensure that the data is persisted correctly. It does this by building an object graph.
(3:Car) -> (2:Radio)
(1:SpecialCar) -> (3:Car)
(1:SpecialCar) -> (2:Radio)
This is one way of representing a graph with three nodes, where: "Car" depends on "Radio", and "SpecialCar" depends on both "Car" and "Radio".
The CLR may represent this as such (specifics not certain from source material):
[Car 3, ref 2], [Radio 2], [SpecialCar 1, ref 3, ref 2] - Abstract Classes
- Access Modifiers
- Anonymous Methods
- Anonymous Types
- Arrays
- Attributes
- Console I/O
- Constructors
- Const Fields
- Delegates
- Enums
- Exceptions
- Extension Methods
- File IO
- Generics
- Interfaces
- Iterators
- LINQ
- Main
- Null Operators
- Parameters
- Polymorphism
- Virtual Functions
- Reflection
- Serialization
- Strings
- Value Types
- "Base" Keyword
- "Is" and "As"
- "Sealed" Keyword
- nameof expression