0
0
CsharpConceptBeginner · 3 min read

What is Serialization in C#: Explanation and Example

In C#, serialization is the process of converting an object into a format that can be easily stored or sent, such as JSON or XML. This allows the object to be saved to a file, sent over a network, or shared between programs, and later deserialized back into the original object.
⚙️

How It Works

Serialization in C# works like packing your belongings into a suitcase before a trip. You take an object, which is like your packed items, and convert it into a format that can be saved or sent, such as a string of text in JSON or XML. This packed format is easy to store on disk or send over the internet.

Later, when you need your belongings again, you unpack the suitcase. This unpacking is called deserialization, where the saved data is converted back into the original object with all its properties and values intact. This process helps programs share data or save their state in a way that can be restored later.

💻

Example

This example shows how to serialize and deserialize a simple C# object to JSON using System.Text.Json. It converts a Person object to a JSON string and then back to a Person object.

csharp
using System;
using System.Text.Json;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Program
{
    public static void Main()
    {
        Person person = new Person { Name = "Alice", Age = 30 };

        // Serialize the person object to JSON
        string jsonString = JsonSerializer.Serialize(person);
        Console.WriteLine("Serialized JSON: " + jsonString);

        // Deserialize the JSON back to a Person object
        Person deserializedPerson = JsonSerializer.Deserialize<Person>(jsonString);
        Console.WriteLine($"Deserialized Person: Name={deserializedPerson.Name}, Age={deserializedPerson.Age}");
    }
}
Output
Serialized JSON: {"Name":"Alice","Age":30} Deserialized Person: Name=Alice, Age=30
🎯

When to Use

Serialization is useful whenever you need to save an object's state or send it to another system. For example, you might serialize user settings to a file so they can be loaded later, or send data between a client and server in a web application.

It is also used in caching, logging, and communication between different parts of a program or different programs altogether. Serialization makes it easy to store complex data in a simple format that can be restored exactly as it was.

Key Points

  • Serialization converts objects into a storable or transferable format.
  • Deserialization restores the original object from the stored data.
  • Common formats include JSON, XML, and binary.
  • C# provides built-in support for serialization with libraries like System.Text.Json and Newtonsoft.Json.
  • Serialization is essential for saving data, communication, and data exchange.

Key Takeaways

Serialization converts C# objects into formats like JSON for storage or transfer.
Deserialization restores objects from these formats back into usable C# objects.
Use serialization to save data, send it over networks, or share between programs.
C# has built-in libraries like System.Text.Json to handle serialization easily.