Serialization is the process of converting an object into a format that can be stored or transmitted, and deserialization is the reverse process. In C#, serialization is often used to save objects to files, send them over a network, or store them in databases. This tutorial will guide you through the basics of serialization in C# using file handling.
Serialization allows you to convert an object into a byte stream, which can then be saved to a file or transmitted over a network. Deserialization converts the byte stream back into an object. C# provides several ways to perform serialization, including binary serialization and XML serialization. In this tutorial, we will focus on binary serialization using the BinaryFormatter class.
First, you need to define a class that you want to serialize. The class must be marked with the [Serializable] attribute.
1using System;23[Serializable]4public class Person5{6public string Name { get; set; }7public int Age { get; set; }89public Person(string name, int age)10{11Name = name;12Age = age;13}14}
To serialize an object to a file, you can use the BinaryFormatter class. Here's how you can do it:
1using System;2using System.IO;3using System.Runtime.Serialization.Formatters.Binary;45public static void SerializeObjectToFile(Person person, string filePath)6{7using (FileStream fs = new FileStream(filePath, FileMode.Create))8{9BinaryFormatter formatter = new BinaryFormatter();10formatter.Serialize(fs, person);11}12}
To deserialize an object from a file, you can use the BinaryFormatter class again. Here's how you can do it:
1using System;2using System.IO;3using System.Runtime.Serialization.Formatters.Binary;45public static Person DeserializeObjectFromFile(string filePath)6{7using (FileStream fs = new FileStream(filePath, FileMode.Open))8{9BinaryFormatter formatter = new BinaryFormatter();10return (Person)formatter.Deserialize(fs);11}12}
Here's a complete example that demonstrates how to serialize and deserialize a Person object:
1using System;23class Program4{5static void Main()6{7Person person = new Person("John Doe", 30);8string filePath = "person.dat";910// Serialize the object to a file11SerializeObjectToFile(person, filePath);12Console.WriteLine("Object serialized successfully.");1314// Deserialize the object from the file15Person deserializedPerson = DeserializeObjectFromFile(filePath);16Console.WriteLine($"Deserialized Object: Name = {deserializedPerson.Name}, Age = {deserializedPerson.Age}");17}1819public static void SerializeObjectToFile(Person person, string filePath)20{21using (FileStream fs = new FileStream(filePath, FileMode.Create))22{23BinaryFormatter formatter = new BinaryFormatter();24formatter.Serialize(fs, person);25}26}2728public static Person DeserializeObjectFromFile(string filePath)29{30using (FileStream fs = new FileStream(filePath, FileMode.Open))31{32BinaryFormatter formatter = new BinaryFormatter();33return (Person)formatter.Deserialize(fs);34}35}36}
Object serialized successfully. Deserialized Object: Name = John Doe, Age = 30
In the next section, we will explore collections in C#, including arrays, lists, and dictionaries. Understanding how to work with collections is essential for building robust applications.
This tutorial should give you a solid understanding of how to serialize and deserialize objects in C# using file handling. Remember that binary serialization is just one way to perform serialization, and there are other methods available depending on your needs.