C#
C# Database Connection: How to connect SQL Server (Example)
Accessing Data from a database is one of the important aspects of any programming language. It is...
The concept of Serialization and deserialization is used whenever data pertaining to objects have to be sent from one application to another. Serialization is used to export application data into a file. The destination application then uses deserialization to extract the data from the application for further use.
Serialization is a concept in which C# class objects are written or serialized to files. Let' say you had a C# class called Tutorial. And the class has 2 properties of ID and Tutorials name.
Serializing can be used to directly write the data properties of the Tutorial class to a file. Deserialization is used to read the data from the file and construct the Tutorial object again.
Let's look at an example of how we can achieve this.
In our example, we are going to perform the below high-level steps in the code
Enter the below code in the program.cs file of the console application.
Step 1) The first step is to add the class which will be used for serialization
Code Explanation:-
Step 2) In this step, first we will create the object of the Tutorial class and serialize it to the file called Example.txt
Code Explanation:-
Step 3) Finally to ensure that the data is present in the file, we use deserialization to deserialize the object from the file.
using System;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
[Serializable]
class Tutorial
{
public int ID;
public String Name;
static void Main(string[] args)
{
Tutorial obj = new Tutorial();
obj.ID = 1;
obj.Name = ".Net";
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(@"E:\ExampleNew.txt",FileMode.Create,FileAccess.Write);
formatter.Serialize(stream, obj);
stream.Close();
stream = new FileStream(@"E:\ExampleNew.txt",FileMode.Open,FileAccess.Read);
Tutorial objnew = (Tutorial)formatter.Deserialize(stream);
Console.WriteLine(objnew.ID);
Console.WriteLine(objnew.Name);
Console.ReadKey();
}
}
}Code Explanation:-
When the above code is set, and the project is run using Visual Studio, you will get the below output.
Output:-
You can see from the above output that the values from the file were deserialized properly and displayed in the console.
Summary
Serialization is used to write class objects to files.
De-Serialization is used to recover the objects from the file.
Accessing Data from a database is one of the important aspects of any programming language. It is...
C# Enumeration An enumeration is used in any programming language to define a constant set of...
C# has a wide array of file operations. These operations include opening a file, reading or...
What is Queue in C#? The Queue is a special case collection which represents a first in first out...
C# Variables A variable is a name given to a storage area that is used to store values of various data...
Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft. It is used...