---
title: "How do I serialize and deserialize objects in C#?"  
description: "How do I serialize and deserialize objects in C#?"  
author: "Rocky Dada"  
published: 2023-09-27  
updated: 2023-09-28  
canonical: https://www.mindstick.com/forum/159975/how-do-i-serialize-and-deserialize-objects-in-c-sharp  
category: "c#"  
tags: ["c#", "asp.net", "api(s)"]  
reading_time: 3 minutes  

---

# How do I serialize and deserialize objects in C#?

How do I serialize and deserialize [objects](https://www.mindstick.com/forum/145447/what-are-objects) in C#?

## Replies

### Reply by Aryan Kumar

Serialization is the process of converting an object's state into a format that can be easily stored or transmitted, such as a file, database, or network stream. Deserialization is the reverse process, where the serialized data is used to recreate the original object. In C#, you can perform serialization and deserialization using various techniques and libraries. Here's an overview of how to do it:

## 1. Using .NET Serialization (BinaryFormatter):

C# provides built-in serialization capabilities using the **System.Runtime.Serialization** namespace. You can use the **BinaryFormatter** class for binary serialization. Here's an example:

```plaintext
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

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

class Program
{
    static void Main()
    {
        // Create an instance of the object
        Person person = new Person { Name = "John", Age = 30 };

        // Serialize the object to a file
        using (FileStream fs = new FileStream("person.dat", FileMode.Create))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(fs, person);
        }

        // Deserialize the object from the file
        using (FileStream fs = new FileStream("person.dat", FileMode.Open))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            Person deserializedPerson = (Person)formatter.Deserialize(fs);
            Console.WriteLine($"Name: {deserializedPerson.Name}, Age: {deserializedPerson.Age}");
        }
    }
}
```

## 2. Using JSON Serialization (Newtonsoft.Json):

JSON (JavaScript Object Notation) is a widely used format for data interchange. You can use libraries like Newtonsoft.Json (Json.NET) for JSON serialization and deserialization. Install the Newtonsoft.Json NuGet package, and use it as follows:

```plaintext
using System;
using Newtonsoft.Json;

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

class Program
{
    static void Main()
    {
        // Create an instance of the object
        Person person = new Person { Name = "John", Age = 30 };

        // Serialize the object to JSON
        string json = JsonConvert.SerializeObject(person);

        // Deserialize the object from JSON
        Person deserializedPerson = JsonConvert.DeserializeObject<Person>(json);
        Console.WriteLine($"Name: {deserializedPerson.Name}, Age: {deserializedPerson.Age}");
    }
}
```

## 3. Using XML Serialization (System.Xml.Serialization):

You can also perform XML serialization using the **System.Xml.Serialization** namespace. Here's an example:

```plaintext
using System;
using System.IO;
using System.Xml.Serialization;

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

class Program
{
    static void Main()
    {
        // Create an instance of the object
        Person person = new Person { Name = "John", Age = 30 };

        // Serialize the object to an XML file
        XmlSerializer serializer = new XmlSerializer(typeof(Person));
        using (FileStream fs = new FileStream("person.xml", FileMode.Create))
        {
            serializer.Serialize(fs, person);
        }

        // Deserialize the object from the XML file
        using (FileStream fs = new FileStream("person.xml", FileMode.Open))
        {
            Person deserializedPerson = (Person)serializer.Deserialize(fs);
            Console.WriteLine($"Name: {deserializedPerson.Name}, Age: {deserializedPerson.Age}");
        }
    }
}
```

These are three common methods for serializing and deserializing objects in C#. Choose the one that best suits your needs and the data interchange format you prefer (binary, JSON, XML, etc.).


---

Original Source: https://www.mindstick.com/forum/159975/how-do-i-serialize-and-deserialize-objects-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
