using System; using System.IO; 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(string[] args) { Person person = new Person { Name = "John", Age = 30 }; MemoryStream stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, person); byte[] data = stream.ToArray(); Console.WriteLine(BitConverter.ToString(data)); } }
using System; using System.IO; 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(string[] args) { byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x4A, 0x6F, 0x68, 0x6E, 0x1E }; MemoryStream stream = new MemoryStream(data); BinaryFormatter formatter = new BinaryFormatter(); Person person = (Person)formatter.Deserialize(stream); Console.WriteLine(person.Name + " " + person.Age); } }In this example, we have deserialized the byte array from the previous example back into a Person object using the Deserialize method of the BinaryFormatter class. In conclusion, System Serialize is a C# package library used for serialization and deserialization of objects. The examples above demonstrate how it can be used to convert an object's state into a byte array, which can then be stored and transported.