C # Save the class instance to a file (class serialization and deserialization)

Source: Internet
Author: User

Sometimes we want to save the class instance for future use. An intuitive method is to write the class as one line, separate each attribute with \ t, and then read it with streamreader.

However, this is too troublesome. There are many lines of code, and you must know the corresponding position of the attribute in the row in advance. At this time, if the class serialization method is used for saving, the code is very simple:

If you have a class, you can add the [serializable] attribute to it, indicating that the class can be serialized.

[Serializable]public class People{    public string Name { get; set; }    public int Age { get; set; }}

Use the following code to serialize the class instance to a file:

// Serialize filestream FS = new filestream (@ "D: \ Program \ CSHARP \ ngramtest \ serializepeople. dat ", filemode. create); People P = new people () {name = "haocheng Wu", age = 24}; binaryformatter BF = new binaryformatter (); BF. serialize (FS, P); FS. close ();

In this way, the above file will save the instance of this class. If you want to read it, you can use

// Deserialization FS = new filestream (@ "D: \ Program \ CSHARP \ ngramtest \ serializepeople. dat ", filemode. open); binaryformatter BF = new binaryformatter (); People P = BF. deserialize (FS) as people;

Using the same method, you can also serialize the list of a class to a file.

// Serialize listfilestream FS = new filestream (@ "D: \ Program \ CSHARP \ ngramtest \ serializepeople. dat ", filemode. create); binaryformatter BF = new binaryformatter (); List <people> PS = new list <people> (); PS. add (new people () {name = "haocheng Wu", age = 24}); PS. add (new people () {name = "James Wu", age = 23}); BF. serialize (FS, PS); FS. close ();

The read method is the same:

// Deserialization listfs = new filestream (@ "D: \ Program \ CSHARP \ ngramtest \ serializepeople. dat ", filemode. open); binaryformatter BF = new binaryformatter (); List <people> PS = BF. deserialize (FS) as list <people>;

Serialization can do a lot of things. Here we just give two simple examples, so it is easy to put them apart.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.