C# Serialization
Serialization is the process of converting an object into a format that can be stored or transmitted. In C#, serialization is commonly used to store or transmit data between different applications or services. In this tutorial, we'll discuss how to serialize and deserialize objects in C#.
Syntax
C# provides two ways to serialize and deserialize objects:
- Using the BinaryFormatter class in the System.Runtime.Serialization.Formatters.Binary namespace.
- Using the DataContractSerializer class in the System.Runtime.Serialization namespace.
Here's the syntax for using the BinaryFormatter class:
using System.Runtime.Serialization.Formatters.Binary;
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
obj = (MyObject)formatter.Deserialize(stream);
And here's the syntax for using the DataContractSerializer class:
using System.Runtime.Serialization;
DataContractSerializer serializer = new DataContractSerializer(typeof(MyObject));
serializer.WriteObject(stream, obj);
obj = (MyObject)serializer.ReadObject(stream);
Example
Let's say we have a class called "Person" that we want to serialize and deserialize. Here's how we can implement it using the BinaryFormatter class:
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
[Serializable]
class Person {
public string Name { get; set; }
public int Age { get; set; }
}
class Program {
static void Main(string[] args) {
Person p = new Person { Name = "John", Age = 40 };
BinaryFormatter formatter = new BinaryFormatter();
// Serialize the object
using (FileStream stream = new FileStream("person.bin", FileMode.Create)) {
formatter.Serialize(stream, p);
}
// Deserialize the object
using (FileStream stream = new FileStream("person.bin", FileMode.Open)) {
Person p2 = (Person)formatter.Deserialize(stream);
Console.WriteLine("Name: " + p2.Name + " Age: " + p2.Age);
}
}
}
And here's how we can implement it using the DataContractSerializer class:
using System.Runtime.Serialization;
using System.IO;
[DataContract]
class Person {
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
}
class Program {
static void Main(string[] args) {
Person p = new Person { Name = "John", Age = 40 };
DataContractSerializer serializer = new DataContractSerializer(typeof(Person));
// Serialize the object
using (FileStream stream = new FileStream("person.xml", FileMode.Create)) {
serializer.WriteObject(stream, p);
}
// Deserialize the object
using (FileStream stream = new FileStream("person.xml", FileMode.Open)) {
Person p2 = (Person)serializer.ReadObject(stream);
Console.WriteLine("Name: " + p2.Name + " Age: " + p2.Age);
}
}
}
Output
When we run the example code above, the output will be:
Name: John Age: 40
This is because we created a new "Person" object, serialized it to a binary or XML file, and then deserialized the file back into a new "Person" object. We then printed the name and age of the deserialized "Person" object to the console.
Explanation
In the examples above, we created a class called "Person" that we want to serialize and deserialize. We then used either the BinaryFormatter class or the DataContractSerializer class to serialize the object to a binary or XML file, respectively. We then deserialized the file back into a new "Person" object and printed its properties to the console.
Use
Serialization is useful when you want to store or transmit data between different applications or services. You can use serialization to convert objects to a format that can be easily stored or transmitted, and then deserialize them back into objects at a later time.
Important Points
- Classes that are to be serialized must be marked as [Serializable] or [DataContract].
- Fields that should not be serialized must be marked as [NonSerialized] or [IgnoreDataMember].
- When using the DataContractSerializer class, all fields that should be serialized must be marked as [DataMember].
Summary
In this tutorial, we discussed how to serialize and deserialize objects in C#. We covered the syntax, example, output, explanation, use, and important points of serialization