Skip to main content

Posts

Showing posts with the label C#-Chapter-5

Chapter 5.2: File I/O and Serialization: Understanding Serialization and Deserialization of Objects

 Introduction: In the previous article , Chapter 5.1, we discussed reading and writing text and binary files, essential skills for working with data in C#. In this article, we'll explore serialization and deserialization of objects, which are important for persisting and restoring object states. In the next article, we'll discuss working with .NET classes, an important aspect of developing C# applications. Serialization: Serialization is the process of converting an object's state into a format that can be stored or transmitted. In C#, the .NET framework provides several serialization mechanisms, such as binary, XML, and JSON serialization: // Binary Serialization BinaryFormatter formatter = new BinaryFormatter(); using (FileStream stream = new FileStream( "data.bin" , FileMode.Create)) { formatter.Serialize(stream, myObject); } Deserialization: Deserialization is the process of reconstructing an object's state from a serialized forma

Chapter 5.1: File I/O and Serialization: Understanding Reading and Writing Text and Binary Files

 Introduction: In this new chapter, we'll explore File I/O and Serialization, essential concepts for working with data in C#. In this first article, we'll discuss reading and writing text and binary files. In the next article, we'll delve into serialization and deserialization of objects, which are important for persisting and restoring object states. Reading and Writing Text Files: C# provides several classes for reading and writing text files, such as StreamReader, StreamWriter, and File: // Writing a text file using (StreamWriter writer = new StreamWriter( "example.txt" )) { writer.WriteLine( "Hello, world!" ); } // Reading a text file using (StreamReader reader = new StreamReader( "example.txt" )) { string line; while ((line = reader.ReadLine()) != null ) { Console.WriteLine(line); } } Reading and Writing Binary Files: For working with binary files, C# provides the BinaryReader and Bina