Home > Backend Development > C#.Net Tutorial > C# object serialization

C# object serialization

PHPz
Release: 2023-08-28 15:29:08
forward
711 people have browsed it

C# 对象序列化

For object serialization, you need to refer to the following code. Here we use the BinaryFormatter.Serialize(stream, reference) method to serialize our example object.

We set up a constructor here -

public Employee(int id, string name, int salary) {
   this.id = id;
   this.name = name;
   this.salary = salary;
}
Copy after login

Now set up the file stream -

FileStream fStream = new FileStream("d:\ew.txt", FileMode.OpenOrCreate);
BinaryFormatter bFormat = new BinaryFormatter();
Copy after login

An object of the Employee class -

Employee emp = new Employee(001, "Jim", 30000);
bFormat.Serialize(fStream, emp);
Copy after login

The above is the detailed content of C# object serialization. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template