Serialization converts an object's state into a stream of bytes so that it can be persisted in a permanent or temporary storage medium When working with applications, you’ll often need to store data in a persistent or non-persistent storage medium so that the same data can be retrieved at a later point of time. Serialization, a feature provided by the CLR, can help you to achieve this. Serialization may be defined as the process of converting an object into a stream of bytes, persisting the object’s state into the memory, database of a file. The reverse of serialization is deserialization, which reconstructs the object from the stream of bytes. In other words, deserialization is the process of converting a serialized object into its original state. Serialization is necessary to pass an object over the wire — it facilitates the transmission of an object over a network. Therefore, you can leverage serialization to pass an object from one application domain to another. You can also take advantage of serialization to create a clone of an object. However, serialization is also costly because of the resource overhead involved in serializing and de-serializing objects. To work with Serialization in .Net you should take advantage of the System.Runtime.Serialization namespace, i.e., you should include this namespace in your program. You can make a class serializable using the [Serializable] attribute. Here’s an example that shows how you can apply this attribute on a class. [Serializable] public class Product { public int productCode; public string productName; } Now, if you would like to restrict one or more members of a class from being serialized, you can use the NonSerialized attribute as shown in the code snippet given below. [Serializable] public class Product { public int productCode; public string productName; [NonSerialized()] public double productPrice; } The .Net framework provides support for the following types of serialization. Binary SOAP XML Custom Binary serialization Binary serialization is the fastest of all the serialization techniques — it can be used to serialize an object to a binary stream. It is a type of serialization that can be used to serialize an object to an output stream while preserving the object’s identity — the type information is not lost in the serialization process. Note that when using binary serialization, the object is saved in its entirety. To work with binary serialization, you should include the System.Runtime.Serialization.Formatters.Binary namespace. SOAP serialization SOAP (Simple Object Access Protocol) serialization is a good choice when you would like to transfer objects from one application to another when these applications use heterogeneous architectures. In essence, the main advantage of using SOAP serialization is portability. SOAP serialization can be used to serialize an object in SOAP format. To work with SOAP serialization you should include the System.Runtime.Serialization.Formatters.Soap namespace in your program. Note that like XML Serialization, objects that are serialized using SOAP serialization are persisted as XML. XML Serialization XML Serialization is a type of serialization that is used to serialize the public members of an instance of a class into a XML stream. Note that XML serialization is slow compared to Binary serialization — actually it is much slower. The primary advantage of XML serializaton is that it provides cross — platform support and because it’s text-based, it is readable and can be edited as well. You can take advantage of XmlAttribute and set it on a property to enable the property to be serialized using XML serialization. The following code snippet illustrates how you can use XmlAttribute on a property. [XmlAttribute("productName")] public string ProductName { get { return productName; } set { productName = value; } } To serialize and de-serialize an object using XML serialization you can use the XmlSerializer. The following code snippet shows how you can serialize an object using XML serialization — note how the XmlSerializer is used. XmlSerializer xmlSerializer = new XmlSerializer(typeof(Product)); using (TextWriter textWriter = new StreamWriter(@"D:Product.xml")) { xmlSerializer.Serialize(textWriter, productObject); } Custom serialization You can leverage custom serialization to control how an instance of a type can be serialized and deserialized. You can implement custom serialization by implementing the ISerializable interface. The ISerializable interface declares the GetObjectData() method. The following code snippet illustrates how you can implement custom serialization technique by implementing the ISerializable interface. [Serializable] public class Product : ISerializable { public void GetObjectData(SerializationInfo info, StreamingContext context) { //Usual code } } Related content feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos