Marking Serializable in C#
What is Marking Serializable in C#?
**Marking a class as Serializable** in C# allows it to be **converted into a format** that can be stored or transmitted. Serialization enables objects to be saved in files, sent over a network, or stored in a database.
Key Features of Marking a Class as Serializable
- Required for **Binary, XML, and JSON serialization**.
- Uses the **[Serializable]** attribute for automatic serialization.
- Can **exclude specific fields** using the **[NonSerialized]** attribute.
- Required when using **BinaryFormatter and SOAP serialization**.
Marking a Class as Serializable
To make a class **serializable**, use the **[Serializable]** attribute.
Example: Marking a Class as Serializable
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable]
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
// Writing Binary Data
class Program
{
static void Main()
{
Person person = new Person { Name = "Alice", Age = 30 };
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream stream = new FileStream("person.dat", FileMode.Create))
{
formatter.Serialize(stream, person);
}
Console.WriteLine("Object serialized successfully.");
}
}
// Output:
// Object serialized successfully.
The **[Serializable]** attribute allows the class to be serialized automatically.
Excluding Fields from Serialization
Some fields should not be serialized, such as **sensitive data, passwords, or derived values**.
Example: Using [NonSerialized] to Exclude Fields
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable]
class User
{
public string Username { get; set; }
[NonSerialized]
private string password;
public User(string username, string password)
{
Username = username;
this.password = password;
}
}
// Writing Binary Data
class Program
{
static void Main()
{
User user = new User("Alice", "SuperSecret123");
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream stream = new FileStream("user.dat", FileMode.Create))
{
formatter.Serialize(stream, user);
}
Console.WriteLine("User object serialized, but password is excluded.");
}
}
// Output:
// User object serialized, but password is excluded.
The **[NonSerialized]** attribute ensures that sensitive data is not serialized.
Marking Serializable for XML Serialization
XML serialization requires the **XmlSerializer** class but does not support private fields.
Example: XML Serialization with [Serializable]
using System;
using System.IO;
using System.Xml.Serialization;
[Serializable]
public class Employee
{
public string Name { get; set; }
public int ID { get; set; }
}
// Writing XML Data
class Program
{
static void Main()
{
Employee emp = new Employee { Name = "Bob", ID = 101 };
XmlSerializer serializer = new XmlSerializer(typeof(Employee));
using (FileStream stream = new FileStream("employee.xml", FileMode.Create))
{
serializer.Serialize(stream, emp);
}
Console.WriteLine("Object serialized to XML.");
}
}
// Output:
// Object serialized to XML.
The **XmlSerializer** does not support **private fields or [NonSerialized] attributes**.
Best Practices for Marking Serializable
- Use **[Serializable]** for **BinaryFormatter and SOAP serialization**.
- Use **[NonSerialized]** to **prevent sensitive fields from being serialized**.
- For **XML or JSON serialization**, prefer **XmlSerializer or JsonSerializer** instead of [Serializable].
- Ensure **version control compatibility** when serializing objects.