C# Object Serialization
Sep 03, 2024 pm 03:05 PMTo convert an object in a sequence or stream of bytes, the process used is called serialization. For the transmission of an object to the databases, file, or memory we use serialization. For the exact recreation or recovery of an object when required, serialization plays a crucial role as it saves the state of an object. Through this statement, we meant that using a web service one can transfer an object to any remote location, by a simple transfer of an object from one domain to another. The reverse process of serialization is known as de-serialization as it is the process of the conversion of a serialized byte sequence into an object.
C# Object Serialization Syntax:
For the serialization of the object in C#, an attribute called [Serializable]. If the attribute is not mentioned in a rightful manner, then at the run time a SerializableException is thrown.
Below is the syntax:
1 2 3 4 5 6 7 8 |
|
C# Object De-serialization Syntax:
Below is the syntax:
1 2 3 4 5 6 7 8 9 10 11 |
|
C# Object Serialization working with Examples
Let us discuss examples of C# Object Serialization.
Example #1
In the code below, we have to serialize the class EduCBA so we have used [Serializable]. To stop any error from occurrence after execution of the code, one should mention this attribute. After mentioning the attribute for the class that we want to serialize, we have described the four properties of our class that are “CourseName1” a string and “CoursePrice1” an integer value and similarly, “CourseName2” a string and “CoursePrice2” an integer value. To open a file “E:EDUCBA.txt” in the read-only mode, an object hello is created an object “hello” and in the end, we have displayed the “yum” using the properties we had mentioned earlier.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
Output:
Example #2
In the code below, we have to serialize the class CBA so we have used [Serializable]. To stop any error from occurrence after execution of the code, one should mention this attribute. After mentioning the attribute for the class that we want to serialize, we have described the nine properties of our class that are “student_ID1” an integer value and “student_name1” a string and “CGPA1” a double value that means it contains number values with decimal points and similarly “student_ID2” an integer value and “student_name2” a string and “CGPA2” a double value and “student_ID3” value is an integer and “student_name3” a string and “CGPA3” a double value. To open a file “E:EDUCBA.txt” in the read-only mode, an object hello is created an object “learn” and in the end, we have displayed the “ID” using the properties we had mentioned earlier within different rows and text mentioning what the data actually represents.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
Output:
Example #3
In the example below, for the serialization of an object firstly we have created a stream ( FileStream) object “boo” then we have created an object for BinaryFormatter object “coo” then we have called “coo.Serialize(boo, aoo)” which is a BinaryFormatter.Serialize() method for the serialization of an object in C#.
Similarly for the de-serialization of an object firstly we have created a stream ( FileStream) object “boo” which is used for reading the serialized output then we have created an object for BinaryFormatter object “doo” then we have called “doo. Deserialize(boo)” which is a BinaryFormatter.Deserialize() method for the de-serialization of an object in C#.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
Output:
Conclusion
On the basis of above discussion, we understood the serialization of an object in C# as well as de-serialization of the same object in C#. We also understood the importance of serialization. We have discussed various examples related to C# serialization and C# de-serialization along with the syntaxes of both.
The above is the detailed content of C# Object Serialization. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
